当前位置:网站首页>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.class
We 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: return
And 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
边栏推荐
- Go language interface learning notes
- Go practice -- design patterns in golang's singleton
- leetcode860. Lemonade change
- Force GCC to compile 32-bit programs on 64 bit platform
- Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
- [clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
- Intégration profonde et alignement des séquences de protéines Google
- Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
- "Hands on deep learning" pytorch edition Chapter II exercise
- [practical project] autonomous web server
猜你喜欢
Go practice - gorilla / handlers used by gorilla web Toolkit
The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
Why is go language particularly popular in China
JQ style, element operation, effect, filtering method and transformation, event object
ES7 easy mistakes in index creation
Ueditor, FCKeditor, kindeditor editor vulnerability
Botu uses peek and poke for IO mapping
appium1.22. Appium inspector after X version needs to be installed separately
Introduction to deep learning (II) -- univariate linear regression
Audio Focus Series: write a demo to understand audio focus and audiomananger
随机推荐
JS scope
[research materials] 2021 China's game industry brand report - Download attached
Objects. Requirenonnull method description
Redis Introduction et explication des types de données
Ueditor, FCKeditor, kindeditor editor vulnerability
[backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
2022-02-11 daily clock in: problem fine brush
Notes | numpy-07 Slice and index
Differences among bio, NiO and AIO
Webapidom get page elements
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
1107 social clusters (30 points)
Online VR model display - 3D visual display solution
Burp suite plug-in based on actual combat uses tips
Basic knowledge of reflection (detailed explanation)
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
Based on RFC 3986 (unified resource descriptor (URI): general syntax)
1087 all roads lead to Rome (30 points)
Covering Safari and edge, almost all mainstream browsers have realized webgl 2.0 support