Initialization effectively achieves as much inertia as possible. Only the. Class syntax is used to obtain the reference to the class. Initialization is not triggered. However, to generate class references, class. forname ()
Initialize immediately
If a static final value is a constant during the compilation period, this value can be obtained without class initialization. However, if a field is set to static or final, it is not enough.
To ensure this behavior, access to it will force class initialization, because it is not a compilation constant
If a static domain is not final, when accessing it, it always requires her to first link (allocate storage space for this domain) and initialize (initialize) before being read.
This bucket)
The preparation for using classes involves three steps:
1. load. This is executed by the class loader. This step searches for bytecode (usually in the path specified by classpath, but this is not required) and creates a class object from these bytecode.
2. Link: In the link phase, the bytecode of the class will be verified to allocate storage space for the static domain. If necessary, all references to other classes created by the class will be parsed.
3. Initialization. If this class has a superclass, initialize it, initialize the static domain and static initialization block.
Initialization is delayed until the static method (the constructor is implicitly static) or the very few static fields are referenced for the first time.
/*****/Package COM. runtech; import Java. util. random; public class demo002 {public static random = new random (47); public static void main (string [] ARGs) {Class C1 = initable1.class; // system is not initialized. out. println (initable1.staticfinal01); // The System of the compiling constant. out. println (initable1.staticfinal02); // enforces class initialization, which is a non-compile-time constant system. out. println ("-----------------------------------"); try {Class C2 = Class. forname ("com. runtech. initable1 "); // The system is initialized directly. out. println (initable2.staticfinal01); system. out. println (initable2.staticfinal02);} catch (classnotfoundexception e) {e. printstacktrace ();} system. out. println ("---------------------------------"); Class C3 = initable3.class; system. out. println (initable3.staticfinal01); // non-compile-time constant system. out. println (initable3.staticfinal02); system. out. println ("---------------------------------"); New initable2 (); system. out. println (initable2.staticfinal01); system. out. println (initable2.staticfinal02) ;}} class initable1 {static final int staticfinal01 = 47; static final int staticfinal02 = demo002.random. nextint (1000); static {system. out. println ("initable1 static");} public initable1 () {system. out. println ("initable1 constructor") ;}} class initable2 {static int staticfinal01 = 12; static final int staticfinal02 = demo002.random. nextint (1000); static {system. out. println ("initable2 static");} public initable2 () {system. out. println ("initable2 constructor") ;}} class initable3 {static int staticfinal01 = 12; static final int staticfinal02 = demo002.random. nextint (1000); static {system. out. println ("initable3 static");} public initable3 () {system. out. println ("initable3 constructor ");}}
Output:
47
Initable1 static
258
-----------------------------------
Initable2 static
12
555
-----------------------------------
Initable3 static
12
693
-----------------------------------
Initable2 Constructor
12
555