Java - Static code block, Constructor code block, Constructor

Static code block: declared with staitc, executed when the JVM loads the class, and executed only once
Constructive code block: defined directly with {} in the class, executed every time an object is created.
Execution order priority: static block, main(), function, construction block, construction method.

Constructor

public HelloA(){//构造函数 } 

Regarding the constructor, the following points should be noted:
1. As soon as the object is created, the corresponding constructor will be called, that is, if the object is not created, the constructor will not run.
2. The role of the constructor is to initialize the object.
3. When an object is created, the constructor only runs once, and the general method can be called multiple times by the object.

Building code blocks

{//构造代码块    
}

There are a few things to note about constructing code blocks:

  1. The function of the construction code block is to initialize the object.
  2. The construction block is run as soon as the object is created, and it takes precedence over the constructor. It should be emphasized here that the construction code block will run only when the object is established. The class cannot call the construction code block, and the execution order of the construction code block and the constructor is that the former is executed before the latter.
  3. The difference between the construction code block and the constructor is: the construction code block is to initialize all objects uniformly, and the constructor is to initialize the corresponding object, because there can be multiple constructors, which constructor is run will establish what kind of object, but no matter which object is created, the same block of construction code is executed first. That is to say, what is defined in the construction code block is the initialization content common to different objects.

static code block

static {//静态代码块 
}

Something to note about static code blocks:

  1. It is executed as the class is loaded, only once, and takes precedence over the main function. Specifically, static code blocks are called by classes. When the class is called, the static code block is executed first, and then the main function is executed.
  2. Static code blocks are actually initialized for classes, while construction code blocks are initialized for objects.
  3. Variables in static code blocks are local variables, which are no different from local variables in ordinary functions.
  4. A class can have multiple static code blocks
public class Test{
   staitc int cnt=6; static{ cnt+=9; } public static void main(String[] args) { System.out.println(cnt); } static{ cnt/=3; } } 运行结果: 5 

Example 1:

public class HelloA {
    public HelloA(){//构造函数 System.out.println("A的构造函数"); } {//构造代码块 System.out.println("A的构造代码块"); } static {//静态代码块 System.out.println("A的静态代码块"); } public static void main(String[] args) { } } 运行结果: A的静态代码块 

Example 2:

public class HelloA {
    public HelloA(){//构造函数 System.out.println("A的构造函数"); } {//构造代码块 System.out.println("A的构造代码块"); } static {//静态代码块 System.out.println("A的静态代码块"); } public static void main(String[] args) { HelloA a=new HelloA(); } } 运行结果: A的静态代码块 A的构造代码块 A的构造函数 

Example 3:

public class HelloA {
    public HelloA(){//构造函数 System.out.println("A的构造函数"); } {//构造代码块 System.out.println("A的构造代码块"); } static {//静态代码块 System.out.println("A的静态代码块"); } public static void main(String[] args) { HelloA a=new HelloA(); HelloA b=new HelloA(); } } 运行结果: A的静态代码块 A的构造代码块 A的构造函数 A的构造代码块 A的构造函数 

For a class, execute in the following order:

  1. execute static code block
  2. execute block of code
  3. execute the constructor

Example 4:

public class HelloA {
    public HelloA(){//构造函数 System.out.println("A的构造函数"); } {//构造代码块 System.out.println("A的构造代码块"); } static {//静态代码块 System.out.println("A的静态代码块"); } } public class HelloB extends HelloA{ public HelloB(){//构造函数 System.out.println("B的构造函数"); } {//构造代码块 System.out.println("B的构造代码块"); } static {//静态代码块 System.out.println("B的静态代码块"); } public static void main(String[] args) { HelloB b=new HelloB(); } } 运行结果: A的静态代码块 B的静态代码块 A的构造代码块 A的构造函数 B的构造代码块 B的构造函数 

When it comes to inheritance, do it in the following order:

  1. Execute the static code block of the parent class and initialize the static member variables of the parent class
  2. Execute the static code block of the subclass and initialize the subclass static member variables
  3. Execute the construction code block of the parent class, execute the constructor of the parent class, and initialize the ordinary member variables of the parent class
  4. Execute the subclass's construction code block, execute the subclass's constructor, and initialize the subclass's ordinary member variables


Author: snoweek
Link: https://www.jianshu.com/p/8a3d0699a923
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325971884&siteId=291194637