当前位置:网站首页>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
边栏推荐
- 联想R7000显卡的拆卸与安装
- The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
- Kept hot standby and haproxy
- JS string and array methods
- Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
- Yolov5 input (II) | CSDN creative punch in
- "250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released
- Notes | numpy-10 Iterative array
- 乾元通多卡聚合路由器的技术解析
- Source insight garbled code solution
猜你喜欢

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

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

Online VR model display - 3D visual display solution

Actual combat 8051 drives 8-bit nixie tube

Detailed explanation of yolov5 training own data set

BTC-密码学原理

leetcode435. Non overlapping interval

Pessimistic lock and optimistic lock of multithreading

Go practice -- design patterns in golang's singleton

Technical analysis of qianyuantong multi card aggregation router
随机推荐
Promise
1115 counting nodes in a BST (30 points)
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
Introduction to rust Foundation (basic type)
Webapidom get page elements
[set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)
Problems encountered in fuzzy query of SQL statements
Intégration profonde et alignement des séquences de protéines Google
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
1103 integer factorization (30 points)
2022-02-12 daily clock in: problem fine brush
Shuttle + alluxio accelerated memory shuffle take-off
Technical analysis of qianyuantong multi card aggregation router
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
Detailed explanation of yolov5 training own data set
Redis breakdown penetration avalanche
Based on RFC 3986 (unified resource descriptor (URI): general syntax)
Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
Introduction to redis and explanation of data types