Java Virtual Machine Specification Although there is no mandatory constraint at when to start the class loading process, but for the initialization of the class, the virtual machine specification is strictly defined to have and only four cases must immediately initialize the class, encounter new, Getstatic, Putstatic or invokestatic This 4 bytecode instruction, if the class has not been initialized, it needs to trigger its initialization first.
The most common Java code scenarios for generating these 4 instructions are:
1) Instantiating an object using the New keyword
2) read a static field of a class (except for static fields that have been final decorated and have placed the result in the constant pool during the compilation period)
3) Set a static field for a class (except for static fields that are final decorated and have the result placed in the constant pool at compile time)
4) Call a static method of a class
Verify:
1) When the class is initialized, its static block of code executes.
Class classloadtime{
static{
System.out.println ("Classloadtime class is initialized when it is executed!") ");
}
Public Classloadtime () {
System.out.println ("Classloadtime constructor! ");
}
}
Class classloaddemo{
public static void Main (string[] args) {
Classloadtime CLT = new Classloadtime ();
}
}
Output Result:
The Classloadtime class is initialized when it is executed!
Classloadtime constructor Function!
2) read a static field of a class (except for static fields that have been final decorated and have placed the result in the constant pool during the compilation period)
Class classloadtime{
static{
System.out.println ("Classloadtime class is initialized when it is executed!") ");
}
public static int max = 200; (Prevent the test class and this class from being in a package, using the public modifier)
Public Classloadtime () {
System.out.println ("Classloadtime constructor! ");
}
}
Class classloaddemo{
public static void Main (string[] args) {
int value = Classloadtime.max;
System.out.println (value);
}
}
Output:
The Classloadtime class is initialized when it is executed!
200
3) Set a static field for a class (except for static fields that are final decorated and have the result placed in the constant pool at compile time)
Class classloadtime{
static{
System.out.println ("Classloadtime class is initialized when it is executed!") ");
}
public static int max = 200; (Prevent the test class and this class from being in a package, using the public modifier)
Public Classloadtime () {
System.out.println ("Classloadtime constructor! ");
}
}
Class classloaddemo{
public static void Main (string[] args) {
Classloadtime.max = 100;
}
}
Output:
The Classloadtime class is initialized when it is executed!
4) Call a static method of a class
Class classloadtime{
static{
System.out.println ("Classloadtime class is initialized when it is executed!") ");
}
public static int max = 200; (Prevent the test class and this class from being in a package, using the public modifier)
Public Classloadtime () {
System.out.println ("Classloadtime constructor! ");
}
public static void Method () {
System.out.println ("static method call! ");
}
}
Class classloaddemo{
public static void Main (string[] args) {
Classloadtime.method ();
}
}
Output:
The Classloadtime class is initialized when it is executed!
The invocation of a static method!
The final decorated static field does not initialize the class when the operation is used because the constant is placed in the constant pool at compile time.
Test:
Class classloadtime{
static{
System.out.println ("Classloadtime class is initialized when it is executed!") ");
}
public static final int MIN = 10; (Prevent the test class and this class from being in a package, using the public modifier)
}
Class classloaddemo{
public static void Main (string[] args) {
System.out.println (classloadtime.min);
}
}
Output:
10
A subclass invokes or sets a static field of a parent class, or invokes a static method of a parent class just to initialize the parent class, without initializing the child class. Constants that also read the final modifier do not initialize the class.
Class fu{
public static int value = 20;
static{
System.out.println ("The parent class did the initialization of the Class!") ");
}
}
Class zi{
static{
System.out.println ("Subclass" Initializes the Class!) ");
}
}
Class loaddemo{
public static void Main (string[] args) {
System.out.println (Zi.value);
}
}
Output:
The parent class did the initialization of the class!
20
The initialization time for various members of the Java class is not tested here:
Initialization time for class variables (static variables), instance variables (non-static variables), static code blocks, non-static blocks of code:
* Modified by the static keyword (such as: Class variable [static variable], static code block) will be initialized before the class is initialized to create the instance object, and is sequentially executed from top to bottom;
public static int value = 34;
static{
System.out.println ("Static code block! ");
}
public class name () {
System.out.println ("Constructor! ");
}
Once this is written, the static field value is initialized before the class is initialized to create the instance object, and then the static code block is executed, and the code in the construction method is executed when the object is instantiated
* Initialization with no static keyword adornments (e.g., instance variables [non-static variables], non-static code blocks) is actually extracted into the constructor of the class, but is more than the class constructor
Code blocks are executed first, and they are executed sequentially from top to bottom.
public int value = 34;
{
SYSTEM.OUT.PRINTLN ("Non-static code block! ");
}
public class name () {
System.out.println ("Constructor! ");
}
When an object is instantiated with a constructor, value is initialized first, then a non-static block of code is executed, and the code inside the constructor is executed.
* When a parent class is present, the constructor of the child class is called, and the parent class's default construct (the null argument construct) is called before the parent class is initialized.
Class loading Time in Java