类的加载过程
类的加载过程
Load:
Link:Verify, Prepare, (Optionally) Resolve
Initialize: Execute Initializers
Prepare:
Preparation involves creating the static
fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
静态属性和对象设置默认值,最后的值在初始化阶段进行赋值。
Resolve:
Before a symbolic reference can be used it must undergo resolution, wherein a symbolic reference is checked to be correct and, typically, replaced with a direct reference that can be more efficiently processed if the reference is used repeatedly.
Initialize:
A class or interface T will be initialized immediately before the first occurrence of
any one of the following:
• T is a class and an instance of T is created.
• A static method declared by T is invoked.
• A static field declared by T is assigned.
• A static field declared by T is used and the field is not a constant variable
(§4.12.4)
class Super {
static int taxi = 1729;
}
class Sub extends Super {
static { System.out.print("Sub "); }
}
class Test {
public static void main(String[] args) {
System.out.println(Sub.taxi);
}
}
This program prints only:
1729