当前位置:网站首页>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
边栏推荐
- 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
- 1114 family property (25 points)
- Self introduction and objectives
- Notes | numpy-09 Broadcast
- Notes | numpy-11 Array operation
- Chapter II program design of circular structure
- 乾元通多卡聚合路由器的技术解析
- Go practice -- gorilla / websocket used by gorilla web Toolkit
- 谷歌 | 蛋白序列的深度嵌入和比对
- [set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
猜你喜欢
![[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)

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

Yolov5 network structure + code + application details | CSDN creation punch in

Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément

BIO、NIO、AIO区别

leetcode435. Non overlapping interval

ES7 easy mistakes in index creation

联想R7000显卡的拆卸与安装

5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip

Principles of BTC cryptography
随机推荐
Common methods of JS array
酒店公共广播背景音乐-基于互联网+的酒店IP网络广播系统设计
Basic knowledge of reflection (detailed explanation)
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
Online VR model display - 3D visual display solution
Messy change of mouse style in win system
JQ style, element operation, effect, filtering method and transformation, event object
Redis expiration elimination mechanism
动态规划——相关概念,(数塔问题)
Silent authorization login and registration of wechat applet
Differences among bio, NiO and AIO
音频焦点系列:手写一个demo理解音频焦点与AudioMananger
Notes | numpy-07 Slice and index
JS string and array methods
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
Common interview questions of microservice
The IntelliJ platform completely disables the log4j component
Botu uses peek and poke for IO mapping
1087 all roads lead to Rome (30 points)
ES7 easy mistakes in index creation