The role of code blocks in Java

The code segment enclosed in { } in Java is the code block, which is divided into the following types

  Location effect
local code block

inside the main method

Define the life cycle of the variable, the variable of the local code block will be recycled by Java after the execution ends
Building code blocks in the member position of the class

Execute the construction code block before each execution of the constructor,

You can put the same code from multiple constructors into a constructor block to initialize an object.

static code block in the member position of the class It is generally used to initialize a class , and the statically decorated code block is executed only once.

Note that the priority of construction code blocks, construction methods, and static code blocks is:

    static code block > construction code block > construction method;

When an object calls a method, first check whether there is a static code block in the class, execute the static code block first, then check whether there is a construction code block, and finally execute the construction method

Example:

class Block{
	{
		System.out.println("I am a construction code block"); //Execute every time it is called by a new object, the priority is after the static code block
	}
	static {
		System.out.println("I am a static code block"); //Only executed at the first call, with the highest priority
	}
	public void method() {
		System.out.println("I am a member method"); 	
	}
	public Block() {
		super();
		System.out.println("I am a no-argument constructor");
	
	}
	
}
public class ARRTEXT {
	public static void main(String[] args) {
		{
			String a="I am a local code block";
			System.out.println(a);
		}
		//System.out.println(a); The variable in the error local code block cannot be accessed outside the code block, and it will be recycled after the code block is executed
		System.out.println();
		Block b=new Block();
		b.method();
		System.out.println();
		Block c=new Block();
		c.method();	
		  
	}
}
/*
The output is:
I am a local code block

i am static code block
I am constructing code blocks
I am a no-argument constructor
i am member method

I am constructing code blocks
I am a no-argument constructor
i am member method
 * */

requires attention:

    Static code blocks and construction code blocks = start execution when the object new is created, and anonymous objects can also be called.

    Static code blocks are generally used for some code that needs to be executed before the class is executed. Similar to static methods, the priority is higher than that of classes. The difference between the two is that static methods need to be called manually through the class name. Method name, while Static code blocks are executed automatically, and the static code blocks in the class are executed before the class is loaded.

inherit:

In inheritance, when the object is initialized, the static code block of the parent class is executed first, then the static code block of the subclass, and then the construction code block and the parameterless construction method of the parent class are executed, and finally the construction code block of the subclass is executed. and a no-argument constructor.

In short, static is the first priority, and the construction code block is before the construction method.

as follows:

class Block{

	{
		System.out.println("I am the parent class construction code block");
	}
	static {
		System.out.println("I am the parent class static code block");  
	}
	public void method() {
		System.out.println("I am a member method"); 	
	}
	public Block() {
		super();
		System.out.println("I am the no-argument constructor of the parent class");
	
	}
	
}
class app extends Block{
	static {
		System.out.println("I am a static code block of a subclass");
	}
	{
		System.out.println("I am the construction code block of the subclass");
	}
	public app() {

		System.out.println("I am a subclass no-argument constructor");
	}
	
}
public class ARRTEXT {
	public static void main(String[] args) {
	
		app a=new app();
	

	}
}
/* The output is:
I am the parent class static code block
I am subclassed static code block
I am the parent class constructing the code block
I am the no-argument constructor of the parent class
i'm the constructor block of the subclass
I am a subclass no-argument constructor

 * */

Guess you like

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