static 类型初始化

在初始化时,先初始化static类型变量,再初始化普通变量,再初始化构造函数。
如下程序:
class A{
A(int i){
System.out.println("A "+i);
}
void f(int i){
System.out.println("f1 "+i);
}
}

class B{
static A a1=new A(1);
B(){
System.out.println(" B ");
a2.f(1);
}
void f2(int i){
System.out.println(" f2 "+i);
}
static A a2=new A(2);
}

class C{
A a3=new A(3);
static A a4=new A(4);
C(){
System.out.println(" C ") ;
a4.f(2);
}
void f3(int i){
System.out.println(" f3 "+i);
}
static A a5=new A(5);
}
public class TestStaticInitial {
public static void main(String args[]){
System.out.println("Now begin to test!");
new C();
System.out.println("Now begin to test the second time!");
new C();
bb.f2(1);
cc.f3(1);
}
static B bb=new B();
static C cc=new C();
}
执行顺序:
在主程序中,先初始化static 变量bb,cc.
再打印Now begin to test!;
执行new C();
再打印Now begin to test the second time
执行new C();

初始化变量bb的过程:
先初始化static 变量a1,a2
再执行B();

[color=red]注意static变量只执行初始化一次[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值