Code block usage in java

Keywords: Attribute

/**
 * Fourth member of class - initialization block (or code block) 
 * 1. If the code block is decorated, only static can be used 
 * 2. Classification:
 *  Non static initialization block 
 * 1. You can initialize the properties (static & non static) of a class, or call the methods declared by this class 
 * 2. There can be output statements in it 
 * 3. A class can have multiple initialization blocks, which are executed in sequence 
 * 4. Every time an object of a class is created, it is loaded with non-static initialization. 
 * 5. The execution of a non static initialization block is earlier than the constructor.
 * 
 *  Static code block 
 * 1. There can be output statements in it 
 * 2.Loaded with the class, and only once 
 * 3.Sequential structure execution between multiple static code blocks
 * 4.The execution of static code block is earlier than that of non static code block 
 * 5. Static code blocks can only execute static structures (attributes, methods)
 *
 * About attribute assignment: 1. Default initialization 2. Displayed initialization or initialization block (where two structures are executed in sequence) 3. In the construction 4. Modify attribute value by method
 */
public class TestOrder {
	public static void main(String[] args) {
		/**
		 * Non static initialization block
		 */
		Order order = new Order();
		System.out.println(order);

		System.out.println();
		System.out.println("*********************");
		System.out.println();

		Order order1 = new Order();
		System.out.println(order1);

		System.out.println();
		System.out.println("*********************");
		System.out.println();

		System.out.println("Output the above two order After the object is found, the output is exactly the same [without static code block], it can be proved that every object that creates a class is loaded without static initialization.");
		System.out.println("For better proof, it's best to remove the assignment in the constructor and only use the initialization block to assign. Two printed order The object remains the same.");
		System.out.println("After adding the static code block, we found that the output of the two order Objects are different. Only the first object has the output of static code block, which proves that static code block is loaded only once");
	}
}

class Order {
	private String name;
	private int age;
	private static String nameOrder;

	public Order() {
		super();
		name = "Wang Wu";
		age = 16;
		System.out.println("This is a parameterless constructor.,new When an object is executed, the last step is to execute the constructor, and the object will new Come out");
	}

	public Order(String name, int age) {
		super();
		this.name = name;
		this.age = age;
		System.out.println("This is a parametric constructor, new When an object is executed, the last step is to execute the constructor, and the object will new Come out");
	}

	static {
		System.out.println("Here is static block 2");
		nameOrder = "Pock";
		// name = "Zhao Yun"; / / error reported, unable to write
	}
	{
		name = "Zhang San";
		age = 14;
		System.out.println("This is the first initialization block, step 1");
	}
	{
		name = "Li Si";
		age = 15;
		System.out.println("This is the second initialization block, step 2");
	}
	static {
		System.out.println("Here is static block 1");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Order [name=" + name + ", age=" + age + "]";
	}

}

Here is static block 2
 Here is static block 1
 This is the first initialization block, step 1
 This is the second initialization block, step 2
 This is a nonparametric constructor. When an object is new, the last step is to execute. After executing the constructor, the object will be new
 Order [name = Wang Wu, age=16]

*********************

This is the first initialization block, step 1
 This is the second initialization block, step 2
 This is a nonparametric constructor. When an object is new, the last step is to execute. After executing the constructor, the object will be new
 Order [name = Wang Wu, age=16]

*********************

After exporting the above two order objects, it is found that the output is exactly the same [without static code blocks]. It can be proved that every object that creates a class is loaded without static initialization.
For better proof, it's best to remove the assignment in the constructor and only use the initialization block to assign. The two order objects printed are still the same.
After adding the static code block, it is found that the two output order objects are different, only the first object has the output content of the static code block, which proves that the static code block is only loaded once

Posted by lazaruz on Sun, 17 Nov 2019 14:55:52 -0800