Java Class Life Cycle - Beginning

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值