Analysis of Common Code Block, Construction Code Block and Static Code Block

Posted by artizan on Sun, 12 May 2019 05:06:56 +0200

After learning code blocks, building code blocks and static code blocks for the first time, I decided to write this article to summarize the learning results in particular. This article describes code blocks in the simplest language. If there are any errors, please ask the gods to point out, thank you very much (bow)

Ordinary code block

Existing in methods, enclosed in {}

This kind of code block is called ordinary code block, which can be executed sequentially just like normal statement execution.

public class A{
   public static void main(String[] args){ 
		{
			System.out.println("I'm the first common code block.");    
		}                 
                System.out.println("Normal code blocks are no different from normal statements");
		{
			System.out.println("I'm the second common code block.");    
		}
   }
}
	
/*
Operation results:
            I'm the first common code block.
            Normal code blocks are no different from normal statements	
            I'm the second common code block.
*/

2. Constructing blocks of code

Existing in the class body, enclosed in {}

Every time an object is created, it is called, and the construction code block is executed before the construction method. Note! If the main method is in the class where the main method is located, the main method will not execute the new object, because the object has not been created at this time. The main method can be called because of the static keyword. Even if there is no new object, the jvm can also be called by the class name! (See specifically why the main method is modified with public static void)

See the following code for specific differences

At this point, the main method has the statement new B2();

public class B2{
	public static void main(String[] args){
		System.out.println("main Method is executed");
                new B();
        }
	public B2(){
		System.out.println("The construction method is executed");
	}
        { 	
		System.out.println("Construction blocks are executed");
        }	
}
	
/*
Operation results:
                        main Method is executed
		        Construction blocks are executed
		        The construction method is executed
*/

Next we delete the statement new B2

          

public class B2{
	public static void main(String[] args){
		System.out.println("main Method is executed");
        //new B2();
        }
	public B2(){
		System.out.println("The construction method is executed");
	}
        { 	
		System.out.println("Construction blocks are executed");
        }	
}
	
/*
Operation results:
                main Method is executed
*/

Output results are only "main method executed"

As you can see, if you need to execute the construction block in the main method's class, you need the object of the new main method's class in the main method.

3. Static code block

Existing in methods, modified by static, and {}enclosed

static{

                        }

Static code blocks are executed as classes are loaded, so static code blocks are executed only once (class files are loaded only once). When class files are interpreted through jvm after compilation, the main method's class is loaded into the method area first, and whether there is a parent class or not is checked.

If (1) the parent static variable is initialized by default, if there is a statement static int a = 1 in the class body, then the default initialization A is 0.

(2) Display initialization of parent static variables. If there is a statement static int a = 1 in the class body, display initialization A is 1

(3) Executing parent static code blocks

(4) Subclass static variables are initialized by default

(5) Display initialization of subclass static variables

(6) Executing static code blocks of subclasses

(7) Execute the main method of the subclass (assuming that the main method has another new object of this kind at this time: if not, it ends at this step)

(8) Default initialization of parent non-static variables

(9) Display initialization of parent non-static variables

(10) Execute parent constructor blocks

(11) Execute the construction method of the parent class

(12) Default initialization of non-static variables of subclasses

(13) Display initialization of non-static variables of subclasses

(14) Constructor blocks that execute subclasses

(15) Executing the construction method of subclasses

                   

The specific code is as follows

                   

class B{
        static int a = 1;
        int a2 = 2;
	static 
	{
                System.out.println("Parent class static variable"+a);
		System.out.println("Parent static code block");
	}
	public B(){
		System.out.println("The parent constructor is executed");
	}
        {
		System.out.println("Parent class is not static variable"+a2);     	
		System.out.println("Parent class constructs code blocks");
        }	
}
class A extends B{
	static int b = 3;
        int b2 = 4;	
	static 
	{
		System.out.println("Subclass static variable"+b);
		System.out.println("Subclass static code block");
	}
	public static void main(String[] args){
    	        System.out.println("main Method is executed");
		new A();
	}
        public A(){
                System.out.println("Subclass constructors are executed");
        }
	{
                System.out.println("Subclass non static variable"+b2);	
		System.out.println("Subclasses construct code blocks");
	}
}	
/*
Operation results:
                    Parent static variable 1
		    Parent static code block
		    Subclass static variable 3
		    Subclass static code block
		    main Method is executed
		    Parent non-static variable 2
		    Parent class constructs code blocks
		    The parent constructor is executed
		    Subclass non-static variable 4
		    Subclasses construct code blocks
                    Subclass constructors are executed
*/

If the main method does not have a parent class in its class, it can be executed regularly

          
From this, we can also summarize the law of class loading: (1) First parent class, then child class.

(2) First static then non-static

(3) Attribute before method

(4) Declaration before assignment

 

This concludes

For the first blog, please give us more advice, one or two, thank you again.

         

Topics: jvm Attribute