Code block usage in java
Keywords:
Attribute
public class TestOrder {
public static void main(String[] args) {
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 = "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