The so called beginning of class life cycle is LOAD CLASS to CLASS INITIALIZE.
1. Load Class
2. Link Class
3. Init Class
JVM only requires the timing of Class Initialize, which is should before the first time use the classinitiatively, and there should be in one of these 6 conditions:
1. create an instance of a class (new, clone, un-serilize..)
2. invoke some static method of a class
3. visit a non-final static field of a class of a interface (final static fields compiled as constants)
(visit a non-final static filed which is defined in super class, is not a initiatively use case)
4. invoke some reflection methods
5. init its child class
6. designate a class as bootstrap class when JVM starts
1. Load Class
(1) generate binary stream stands for this class
(2) parse this stream into data structure of Method Area of JVM
(3) create a Class instance for this class in Heap Area of JVM
2. Link Class
(1) Verify class
check something like a final class can't be inherited, final method can't be overrided...
(2) Prepare class
allocate memory for this class, give each field default value
(3) Resolve class (optional)
It's an optional process, is to replace constants with direct reference
3. Init Class
Execute <clinit> method if this class has no super class, if yes, should execute <clinit> of super class first. <clinit> method is to run static block of a class, and assign initial value for fields.
Example:
When we use JDBC connection, always use Class.forName("class driver class string"). This is to initialize driver class.
We can also use NEW to initialize class, but
1. we don't need the instance of driver class, instantiate driver class will spare space in Heap Area of JVM
2. use forName other than new is more flexible to load a DB driver. (Reference: http://blog.sina.com.cn/s/blog_60f823dd0100ozuz.html)