1.类中的static变量在类加载时首先被初始化,且只能初始化一次.
2.所有对象共享类中的static变量.
3.在创建子类的对象时,先初始化父类的static变量,再初始化子类的static.
实例如下:
public class A
{
static
{
System.out.println("I'm in class A's static!");
}
public A()
{
System.out.println("I'm in class A's structure!");
}
};
public class B extends A
{
static
{
System.out.println("I'm in class B's static!");
}
public B()
{
System.out.println("I'm in class B's structure!");
}
};
public class TestStatic
{
public static void main(String[] arg)
{
A ab = new B();
B ba = new B();
}
};
输出结果为:
I'm in class A's static!
I'm in class B's static!
I'm in class A's structure!
I'm in class B's structure!
I'm in class A's structure!
I'm in class B's structure!