Virtual machine loading Java classes, in a certain order to load, understand the virtual machine loading sequence, can help us better to solve the problem, the following we see through the actual phenomenon of how the virtual machine loading and initializing the class.
To get a better view of the initialization process, we first create a log class to output initialization information:
Package com.jianggujin.loadeorder;
public class log
{public
log (String msg)
{
System.err.println (msg);
}
}
When the Log class object initializes, it outputs the relevant information directly.
Next, we create the parent and child classes individually:
Parent.java
Package com.jianggujin.loadeorder;
public class parent
{
Log log1 = new log ("Parent generic attribute");
static Log log2 = new log ("Parent class static Property");
Static
{
System.err.println ("Parent class static code block");
}
Public parent ()
{
System.err.println ("Parent construction Method");
}
Child.java
Package com.jianggujin.loadeorder;
public class Child extends Parent
{
log log1 = new Log ("Subclass generic Attribute");
Static
{
System.err.println ("Subclass Static code block");
}
static Log log2 = new log ("Subclass static Property");
Public Child ()
{
System.err.println ("Subclass construction Method");
}
In these two classes, we output the information in common properties, static attributes, static blocks of code, and construction methods, and finally write the test class for testing:
Package com.jianggujin.loadeorder;
public class Maintest
{public
static void Main (string[] args)
{
new Child ();
}
}
Run the program and watch the console output information
With the console information, we can see the loading of the class and the order of the initialization of the property, and we conclude that the order in which the Java classes are loaded in the JVM virtual machine is:
Parent class static Property/parent class static code block-> subclass Static code block/Subclass static property-> Parent Common attribute-> Parent class construction method-> Subclass Common attribute-> Subclass construction Method
Knowing the loading order of the classes, let's explore an object that declares no value only. What is the difference between a null value in practice and a lot of people who think that an object that has not been initialized defaults to NULL, as if it doesn't make any difference, and here we look at a usage scenario where there is an init () in the parent class. method is used to initialize the work, and the method is invoked in the constructor method of the parent class, and the Init () method is overridden in the subclass to initialize the subclass member variables in the method, and we see if the effect is the same:
Parent.java
Package com.jianggujin.loadeorder;
public class parent
{public
parent ()
{
init ();
}
public void init ()
{
}
}
Child.java
Package com.jianggujin.loadeorder;
public class Child extends Parent
{
Object obj1;
Object obj2 = null;
@Override public
void init ()
{
System.err.println ("Subclass Init Method");
Obj1 = new Object ();
Obj2 = new Object ();
}
Write test methods for testing:
Package com.jianggujin.loadeorder;
public class Maintest
{public
static void Main (string[] args)
{child child
= new Child ();
System.err.println (child.obj1);
System.err.println (CHILD.OBJ2);
}
Watch the console and we'll see output similar to the following:
java.lang.object@15db9742
Null
From this, we can find that only declaring no value is different from assigning null values.