当前位置:网站首页>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
边栏推荐
- Introduction to webrtc protocol -- an article to understand dtls, SRTP, srtcp
- Notes | numpy-09 Broadcast
- [set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)
- 2022-02-12 daily clock in: problem fine brush
- Differences among bio, NiO and AIO
- [research materials] 2021 China's game industry brand report - Download attached
- 大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
- How to connect the network: Chapter 1 CSDN creation punch in
- "Pthread.h" not found problem encountered in compiling GCC
- Shuttle + alluxio accelerated memory shuffle take-off
猜你喜欢

Technical analysis of qianyuantong multi card aggregation router

Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn

【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)

cookie session jwt

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

SSM framework integration

联想R7000显卡的拆卸与安装

Shallow and first code
![[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)](/img/ce/d6f4fb30727e7436b6443537429ad4.png)
[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)

Oracle SQL table data loss
随机推荐
最大连续子段和(动态规划,递归,递推)
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
Make your own dataset
Bluebridge cup real topic 2020 palindrome date simulation construction provincial competition
appium1.22.x 版本后的 appium inspector 需单独安装
1110 complete binary tree (25 points)
Gbase8s composite index (I)
ES7 easy mistakes in index creation
Principles of BTC cryptography
1095 cars on campus (30 points)
JS function algorithm interview case
Wechat applet distance and map
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
1086 tree traversals again (25 points)
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip
酒店公共广播背景音乐-基于互联网+的酒店IP网络广播系统设计
Interview question -- output the same characters in two character arrays
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
Learn libcef together -- set cookies for your browser