当前位置:网站首页>Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
2022-07-03 05:16:00 【A wild man about to succeed】
Here's the code section
public class InitializeDemo {
private static int k = 1;
private static InitializeDemo t1 = new InitializeDemo("t1");
private static InitializeDemo t2 = new InitializeDemo("t2");
private static int i = print("i");
private static int n = 99;
{
print(" Initialization block ");
j = 100;
}
public InitializeDemo(String str) {
System.out.println((k++) + ":" + str + " i=" + i + " n=" + n);
++i;
++n;
}
static {
print(" A static block ");
n = 100;
}
private int j = print("j");
public static int print(String str) {
System.out.println((k++) + ":" + str + " i=" + i + " n=" + n);
++n;
return ++i;
}
public static void main(String[] args) {
InitializeDemo test = new InitializeDemo("test");
}
}
The output is as follows
1: Initialization block i=0 n=0
2:j i=1 n=1
3:t1 i=2 n=2
4: Initialization block i=3 n=3
5:j i=4 n=4
6:t2 i=5 n=5
7:i i=6 n=6
8: A static block i=7 n=99
9: Initialization block i=8 n=100
10:j i=9 n=101
11:test i=10 n=102
Process finished with exit code 0
First we need to know , Run a class
1. First initialize static variables and static code blocks ( No matter who calls this object later , It will only be initialized once ):
The priority of static variables is the same as that of static code blocks . therefore Who is in front of whom , Who initializes . We usually write variables first , Then write static code blocks , That is to say, first run the initialization of static variables , Then run the initialization of the static code block ( This will avoid mistakes ===>java: Illegal forward reference ), as follows
static {
haha=1;// You can assign
System.out.println(haha);// But it can't output , Will be displayed ==>java: Illegal forward reference
}
private static int haha;2. after initialization Variables and code blocks , And then in initialization Constructors :
Variables and code blocks have the same priority ( Who is ahead , Who initializes ), But it takes precedence over the initialization of the constructor . in other words , No matter where the constructor is , Are initialized at last . Because they are not static , So they exist in the object ,new An object will be initialized once .
The following is the initialization zero value of the basic data type :( come from 《 Deepen understanding java virtual machine 》7.3.3 Table in Chapter 7-1)
| int | byte | short | long | boolean | float | double | char | reference |
| 0 | (byte)0 | (short)0 | 0L | false | 0.0f | 0.0d | '\u0000' | null |
that , The above code flow example is
- First of all k assignment 1
- then new object t1
- Execute the initialization block first
- perform private int j = print("j");
- Execute constructor
- continue new object t2
- Execute the initialization block first
- perform private int j = print("j");
- Execute constructor
- perform private static int i = print("i");
- to n assignment 99
- Execute static block
- perform main Method
- new object test
- Execute the initialization block first
- perform private int j = print("j");
- Execute constructor
- new object test
Confused, isn't it , Then let's continue to dig deep into the bottom
Next, we analyze according to decompilation
Let's decompile the bytecode file , The execution code is as follows
javap -c InitializeDemo.classWe will get the following source code
public class com.heaboy.mvc.InitializeDemo {
public com.heaboy.mvc.InitializeDemo(java.lang.String);
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: ldc #2 // String Initialization block
6: invokestatic #3 // Method print:(Ljava/lang/String;)I
9: pop
10: aload_0
11: bipush 100
13: putfield #4 // Field j:I
16: aload_0
17: ldc #5 // String j
19: invokestatic #3 // Method print:(Ljava/lang/String;)I
22: putfield #4 // Field j:I
25: getstatic #6 // Field java/lang/System.out:Ljava/io/PrintStream;
28: new #7 // class java/lang/StringBuilder
31: dup
32: invokespecial #8 // Method java/lang/StringBuilder."<init>":()V
35: getstatic #9 // Field k:I
38: dup
39: iconst_1
40: iadd
41: putstatic #9 // Field k:I
44: invokevirtual #10 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
47: ldc #11 // String :
49: invokevirtual #12 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
52: aload_1
53: invokevirtual #12 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
56: ldc #13 // String i=
58: invokevirtual #12 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
61: getstatic #14 // Field i:I
64: invokevirtual #10 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
67: ldc #15 // String n=
69: invokevirtual #12 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
72: getstatic #16 // Field n:I
75: invokevirtual #10 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
78: invokevirtual #17 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
81: invokevirtual #18 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
84: getstatic #14 // Field i:I
87: iconst_1
88: iadd
89: putstatic #14 // Field i:I
92: getstatic #16 // Field n:I
95: iconst_1
96: iadd
97: putstatic #16 // Field n:I
100: return
public static int print(java.lang.String);
Code:
0: getstatic #6 // Field java/lang/System.out:Ljava/io/PrintStream;
3: new #7 // class java/lang/StringBuilder
6: dup
7: invokespecial #8 // Method java/lang/StringBuilder."<init>":()V
10: getstatic #9 // Field k:I
13: dup
14: iconst_1
15: iadd
16: putstatic #9 // Field k:I
19: invokevirtual #10 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
22: ldc #11 // String :
24: invokevirtual #12 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
27: aload_0
28: invokevirtual #12 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
31: ldc #13 // String i=
33: invokevirtual #12 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
36: getstatic #14 // Field i:I
39: invokevirtual #10 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
42: ldc #15 // String n=
44: invokevirtual #12 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
47: getstatic #16 // Field n:I
50: invokevirtual #10 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
53: invokevirtual #17 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
56: invokevirtual #18 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
59: getstatic #16 // Field n:I
62: iconst_1
63: iadd
64: putstatic #16 // Field n:I
67: getstatic #14 // Field i:I
70: iconst_1
71: iadd
72: dup
73: putstatic #14 // Field i:I
76: ireturn
public static void main(java.lang.String[]);
Code:
0: new #19 // class com/heaboy/mvc/InitializeDemo
3: dup
4: ldc #20 // String test
6: invokespecial #21 // Method "<init>":(Ljava/lang/String;)V
9: astore_1
10: return
static {};
Code:
0: iconst_1
1: putstatic #9 // Field k:I
4: new #19 // class com/heaboy/mvc/InitializeDemo
7: dup
8: ldc #22 // String t1
10: invokespecial #21 // Method "<init>":(Ljava/lang/String;)V
13: putstatic #23 // Field t1:Lcom/heaboy/mvc/InitializeDemo;
16: new #19 // class com/heaboy/mvc/InitializeDemo
19: dup
20: ldc #24 // String t2
22: invokespecial #21 // Method "<init>":(Ljava/lang/String;)V
25: putstatic #25 // Field t2:Lcom/heaboy/mvc/InitializeDemo;
28: ldc #26 // String i
30: invokestatic #3 // Method print:(Ljava/lang/String;)I
33: putstatic #14 // Field i:I
36: bipush 99
38: putstatic #16 // Field n:I
41: ldc #27 // String A static block
43: invokestatic #3 // Method print:(Ljava/lang/String;)I
46: pop
47: bipush 100
49: putstatic #16 // Field n:I
52: returnAnd you can see that :
The first thing we run is static Statically decorated variable code block , Then the main method , Then there are non static variables and non static code blocks and constructors
private static int k = 1;// Field k:I
private static InitializeDemo t1 = new InitializeDemo("t1"); // String t1, Method "<init>":(Ljava/lang/String;)V
private static InitializeDemo t2 = new InitializeDemo("t2"); // String t2, Method "<init>":(Ljava/lang/String;)V
private static int i = print("i");// String i, Method print:(Ljava/lang/String;)I
private static int n = 99;// Field n:I
static {// String A static block
print(" A static block ");
n = 100;
}
public static int print(String str) {// Method print:(Ljava/lang/String;)I, When called, use
System.out.println((k++) + ":" + str + " i=" + i + " n=" + n);
++n;
return ++i;
}And then main Method
public static void main(String[] args) {
InitializeDemo test = new InitializeDemo("test");
}This concludes the procedure
边栏推荐
- 1115 counting nodes in a BST (30 points)
- Kept hot standby and haproxy
- Actual combat 8051 drives 8-bit nixie tube
- Go language interface learning notes
- 1086 tree traversals again (25 points)
- Yolov5 model construction source code details | CSDN creation punch in
- 小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
- [clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
- Audio Focus Series: write a demo to understand audio focus and audiomananger
- Three representations of signed numbers: original code, inverse code and complement code
猜你喜欢

(perfect solution) how to set the position of Matplotlib legend freely

(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)

2022-02-12 daily clock in: problem fine brush

Oracle SQL table data loss

Class loading mechanism (detailed explanation of the whole process)

JS scope
![[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)](/img/af/8dfa783c87363a9d75c52e7680d508.jpg)
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)

Web APIs exclusivity

Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
![[research materials] annual report of China's pension market in 2021 - Download attached](/img/24/622aeeb38de16ac84128b362ceeddb.jpg)
[research materials] annual report of China's pension market in 2021 - Download attached
随机推荐
Silent authorization login and registration of wechat applet
es7创建索引容易犯的错误
Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians
Go practice - gorilla / handlers used by gorilla web Toolkit
Botu uses peek and poke for IO mapping
Messy change of mouse style in win system
appium1.22. Appium inspector after X version needs to be installed separately
Kept hot standby and haproxy
Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn
Introduction to redis and explanation of data types
Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
Dynamic programming - related concepts, (tower problem)
The IntelliJ platform completely disables the log4j component
ES7 easy mistakes in index creation
Introduction to webrtc protocol -- an article to understand dtls, SRTP, srtcp
Go practice -- use redis in golang (redis and go redis / redis)
1118 birds in forest (25 points)
"Pthread.h" not found problem encountered in compiling GCC
[research materials] 2021 China's game industry brand report - Download attached
Common interview questions of microservice