类的初始化:在初始化阶段,java虚拟机执行类的初始化语句,为类的静态变量赋予初始值。在程序中,静态变量初始化有两种途径:
1、类的初始化:在初始化阶段,java虚拟机执行类的初始化语句,为类的静态变量赋予初始值。在程序中,静态变量初始化有两种途径:
(1)在静态变量的声明处进行初始化;
(2)在静态代码快中进行初始化。例如在以下代码中,静态变量a和b都被显示初始化,而静态变量c没有被显示初始化,它将保持默认值为0;
public class ClassLoaderTest {
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
System.out.println("counter1= " + singleton.counter1);
System.out.println("counter2= " + singleton.counter2);
}
}
/**
* 程序是从上向下顺序执行 new Singleton()时,counter1,counter2初始值均为0
* 在通过构造方法Singleton(),均加1.则返回的值counter1,counter2均为1
* 然后再程序在继续向下执行,由于counter1没有显示初始化,则值还是为1 但是counter2经过显示初始化后,其值为0
*
* @author zhaojw_420
*/
class Singleton {
private static Singleton singleton = new Singleton();
public static int counter1;
public static int counter2 = 0;
private Singleton() {
counter1++;
counter2++;
}
public static Singleton getInstance() {
return singleton;
}
}
public class ClassLoaderTest {
public static void main(String[] args) {
System.err.println(Test1.X);
}
}
/**
* 当X=6/3时,编译时即可算出X=2,即编译时常量,即不需要运行类,所以不打印静态代码块中的内容 当X=new
* Random().nextInt(100)时,编译时不能算出X的值,只有运行程序才知道,所以打印结果为:FinalTest static final 2
*
* @author zhaojw_420
*
*/
class Test1 {
//public static final int X=6/3;//打印结果: 2
public static final int X=new Random().nextInt(100);//打印结果为FinalTest static final 2
static{
System.err.println("FinalTest static final");
}
}
类的初始化时机:当java虚拟机初始化一个类时,要求他的所有父类都已经被初始化,但是这条跪着并不适用于接口。
(1)在初始化一个类时,并不先初始化它所实现的接口
(2)在初始化一个接口时,并不会先初始化他的父接口。
因此,一个父接口并不会因为他的子接口或者实现类的初始化而初始化。只有当程序首次使用特定接口的静态变量时,才会导致该接口的初始化。
public class ClassLoaderTest {
static {
System.err.println("Test4 static block");
}
/**运行结果
* Test4 static block
* Parent static block
* Child static block
* 4
* @author zhaojw_420
*
*/
public static void main(String[] args) {
System.err.println(Child.b);
}
}
class Parent{
public static final int a=3;
static{
System.err.println("Parent static block");
}
}
class Child extends Parent{
public static final int b=4;
static{
System.err.println("Child static block");
}
}