Class initialization -- each constructor and code block experiment

       When creating a class, parameters and constructors, and perhaps code blocks, are essential. There are two classes below, which are executed separately to see the effect and analyze.

       father:

class parent{
	int p = 0;
	{
		p=1;
		System.out.println("parent----daimakuai    p="+p);
	}
	static {
		System.out.println("parent----static");
	}
	public parent(){
		System.out.println("parent------gouzao-- p ="+p);
	}
	public parent(String s){
		System.out.println("parent------gouzaos-- p ="+p);
	}
}

 Subclass:

class child extends parent{
	// The following code reports an error
//	{
//		c = 1;
//		System.out.println("child----daimakuai    c="+c);
//	}	
	static{
		System.out.println("child----static");
	}
	int c = 0;
	{
		c = 1;
		System.out.println("child----daimakuai    c="+c);
	}
	public child(){
		System.out.println("child------gouzao-- c ="+c);
	}
	public child(String s){
		System.out.println("child------gouzaos-- c ="+c);
	}
}

 The main function executed for the first time:

 

 

public static void main(String[] a){
		
		child c = new child("abc");
	}

 result:

parent----static
child----static
parent----daimakuai    p=1
parent------gouzao-- p=1
child----daimakuai    c=1
child------gouzaos-- c =1

 It can be seen that his execution order is as follows: static code block of parent class - "static code block of subclass - "normal code block of parent class - "default constructor of parent class - "normal code block of subclass - "child The class's constructor that should be called. An error is reported in the code block found in the child. This is because the code block can initialize the parameters of the class, although it can be above the parameters, but when compiling, the jvm is compiled in order, so an error will be reported (personal point of view, the information is unsuccessful. ).

     If the default constructor of the parent class is removed, the constructor of the subclass will report an error: Implicit super constructor parent() is undefined. Must explicitly invoke another constructor

     This is because, in the constructor of the subclass, the constructor of the superclass is called. If not specified, the default no-argument constructor of the parent class is called. What if the editor can't find the no-argument constructor? report an error. . . .

    Adding the specified constructor to the two constructors of the subclass will not report an error!

public child(){
		//must be added on the first line
		super("");
		System.out.println("child------gouzao-- c ="+c);
	}
	public child(String s){
		super("");
		System.out.println("child------gouzaos-- c ="+c);
		
	}

 

Continue to execute the main function

public static void main(String[] a){
		parent p = new parent("abc");
		child c = new child("abc");
		child c1 = new child("abc");
	}

 result:

parent----static
parent----daimakuai    p=1
parent------gouzaos-- p =1
child----static
parent----daimakuai    p=1
parent------gouzao-- p=1
child----daimakuai    c=1
child------gouzaos-- c =1
parent----daimakuai    p=1
parent------gouzao-- p=1
child----daimakuai    c=1
child------gouzaos-- c =1

 It is found that the difference from the first time is that the code in the static code block is only executed once.

 

   Static is a magical keyword in java, and it is also one of the most commonly used keywords when doing engineering.

 

Guess you like

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