Copy Code code as follows:
Class Parent {
static variables
public static String P_staticfield = "Parent class-static variable";
Variables (in fact, this is better for objects with this, such as writing a class-specific instance)
If this variable is placed behind the initialization block, it will be an error, because you are not initialized at all.
Public String P_field = "Parent class-variable";
Static initialization block
static {
System.out.println (P_staticfield);
System.out.println ("Parent class-static initialization block");
}
Initializing blocks
{
System.out.println (P_field);
System.out.println ("Parent class-Initialization block");
}
Construction device
Public Parent () {
System.out.println ("Parent class-constructor");
}
}
public class Subclass extends Parent {
static variables
public static String S_staticfield = "Subclass--static variable";
Variable
Public String S_field = "Subclass--variable";
Static initialization block
static {
System.out.println (S_staticfield);
System.out.println ("Subclass--static initialization block");
}
Initializing blocks
{
System.out.println (S_field);
System.out.println ("subclass--initialization block");
}
Construction device
Public Subclass () {
Super ();
System.out.println ("Subclass--constructor");
}
Program entry
public static void Main (string[] args) {
System.out.println ("*************in main***************");
New Subclass ();
System.out.println ("*************second subclass***************");
New Subclass ();
}
}
Output results
Parent class--Static variables
Parent class--static initialization block
Subclasses--Static variables
Subclass--Static initialization block
In main***************
Parent class--variable
Parent class--Initialize block
Parent class--builder
Subclasses--variables
Subclass--Initialize block
Subclass--Builder
Second subclass***************
Parent class--variable
Parent class--Initialize block
Parent class--builder
Subclasses--variables
Subclass--Initialize block
Subclass--Builder
Results Analysis:
Obviously after loading the main method, the static variable executes regardless of the parent class or subclass, and then the normal variables and constructors of the parent class and subclass. This is because, when you want to create a subclass of this object, found that this class requires a parent class, so the. Class of the parent class is loaded in, then initializes its normal variable and initializes the code block, and finally its constructor, and then it can start the work of the subclass, and the handle class. Class is loaded in, doing the subclass work.
In addition, in the Java neutron class there is a default constructor called Super () that invokes the parent class, and when there is only a default constructor
Java did it for you, we can do an experiment, if the default constructor is commented out in the parent class, plus a parameter constructor, if
Subclass without Super (argument), a syntax error will be reported at this time
If we annotate all the contents of the main method, you will find that only the output
Parent class--Static variables
Parent class--static initialization block
Subclasses--Static variables
Subclass--Static initialization block
The others will not be exported. What's the reason? Also to study