Detailed explanation of Java code block and code loading sequence principle
This article mainly introduces the detailed explanation of Java code block and code loading sequence principle. The example code is introduced in great detail, which has a certain reference value for everyone's study or work. Friends in need can refer to it
This paper first introduces several basic ranking, and then introduces the characteristics and usage of three code blocks.
When interviewing large companies, if you encounter large state-owned enterprises or large Internet private enterprises, you often encounter written questions about code blocks and code loading order in the written examination. Here is a summary, which is also convenient for you to drag racing without floating.
Noun interpretation
Code block
The code wrapped by {} is called a code block
Static code block
The code wrapped by static {} is called static code block.
Examples of variable definitions of different types:
class Demo{ String x;// 非静态成员变量,又称为属性,对该类不同的对象来说,属性互不相同 static int y = 32;// 类变量,一个类中只有一个该变量,该类不同的对象共享同一个静态成员变量 public static void main(String[] args){ int z = 0;// 局部变量,只在方法内部可见,在方法结束后由垃圾收集器自动回收 } }
Local code block
Location: local location (inside the method).
Function: limit the life cycle of variables, release them as soon as possible and save memory.
Call: executed when the method in which it is called.
The local code blocks in the method are generally called at one time, and the space is released immediately after the call, so as to avoid occupying stack space in the next call process. The stack space memory is limited, and method calls may generate many local variables, resulting in insufficient stack memory. Using local code blocks can avoid this defect.
public class 局部代码块 { public static void go() { // 局部代码块 { int age = 30; System.out.print("go: " + age); } } public static void main(String[] args) { go(); } }
Construct code block
Location: the location of the class member, that is, the location outside the method in the class.
Function: extract the common parts of multiple construction methods and share the construction code block.
Call: each time a construction method is called, it will be executed prior to the construction method, that is, it will be called automatically every time an object is new to realize object initialization.
public class A { int i = 1; int initValue;//成员变量,初始化交给代码块来完成 A(){ System.out.println("构造方法在代码块执行后运行"); } { System.out.println("代码块从上至下依次运行"); //代码块的作用体现于此:在调用构造方法之前,用某段代码对成员变量进行初始化。 //而不是在构造方法调用时再进行。 for (int i = 0;i < 100;i ++) { initValue += i; } } { System.out.println(initValue); System.out.println(i);//此时会打印1 int i = 2;//局部变量,和成员变量不冲突,但会优先使用代码块的变量 System.out.println(i);//此时打印2 //System.out.println(j);//提示非法向后引用,因为此时j的的初始化还没开始。 } int j = 2; { System.out.println(j); System.out.println(i);//代码块中的变量运行后自动释放,不会影响代码块之外的代码 } } public class 构造代码块 { @Test public void test() { A a = new A(); } }
results of enforcement
代码块从上至下依次运行 1 2 构造方法在代码块执行后运行
Static code block
Location: class member location.
Function: initialize the class and load it only once. When new multiple objects, only the static code block will be called for the first time, because the static code block is the same as the class variable,
It belongs to class, and all objects share one.
Call: automatically called when new an object.
public class 静态代码块 { @Test public void test() { C c1 = new C(); C c2 = new C(); //结果,静态代码块只会调用一次,类的所有对象共享该代码块 System.out.println("我是普通方法"); } } class C{ C(){ System.out.println("构造方法调用"); } { System.out.println("代码块调用"); } static { System.out.println("静态代码块调用"); } }
Call result:
静态代码块调用 代码块调用 构造方法调用 代码块调用 构造方法调用 我是普通方法
Execution order static code block - > construction code block - > construction method
Written test questions
Write the following program output results:
public class HelloA { public HelloA(){ System.out.println("HelloA"); } { System.out.println("I'm A class"); } static { System.out.println("static A"); } } public class HelloB extends HelloA { public HelloB(){ System.out.println("HelloB"); } { System.out.println("I'm B class"); } static { System.out.println("static B"); } public static void main(String[] args) { new HelloB(); } }
Execution results:
Analysis: first of all, you should know that static code blocks are loaded with the loading of classes, while construction code blocks and construction methods are loaded with the creation of objects.
1. Compile hellob In Java, since hellob inherits helloa and loads helloa class first, the static code block of helloa class is executed first, then hellob class is loaded, and the static code block of hellob class is executed. There is nothing to say.
2. Then create the hellob object. Everyone knows that constructing code blocks takes precedence over the execution of construction methods. At this time, the problem comes. At this time, we should first look at the construction methods of hellob class. In the construction methods of hellob class, an implicit super() is executed first, so find the construction methods of helloa class, In the construction method of helloa class, there is also an implicit super () execution (calling the construction method of object class), and there is no return result. Next, before executing the method body of helloa class construction method, execute the construction code block of helloa class (I'm a class), then execute the method body of helloa class construction method (i.e. Hello a), and finally return to the construction method of hellob class, At this time, the super () of hellob class has been executed. Before executing the method body of hellob class construction method, execute the construction code block of hellob class (I'm B class), and then execute the method body of subclass construction method (hellb).
No inheritance initialization order:
There is an inherited initialization sequence:
Next, let's take a look at Ali's written test:
public class B{ public static B t1 = new B(); public static B t2 = new B(); { System.out.println("构造块"); } static { System.out.println("静态块"); } public static void main(String[] args) { B t =new B(); } }
Execution results: