百木园-与人分享,
就是让自己快乐。

Java中成员的调用以及static和调用顺序

输出
开始
c
B0
1
A1
F1
D
E
1=1
2=1
3=4


package test2;

class StaticCLass{
    static {
        System.out.println(\"c\");
    }
    static int count1=0;
    private static StaticCLass staticClass =new StaticCLass();
    static {
        System.out.println(\"A\"+count1);
    }
    static int count2 = 1;
    static {
        System.out.println(\"F\"+count2);
    }
    static int count3=3;
    static {
        System.out.println(\"D\");
        count3++;
    }
      private StaticCLass(){
        System.out.println(\"B\"+count2);
        count1++;
        count2++;
          System.out.println(count2);
          count2=100;
        ++count3;
    }
    static StaticCLass getInstance(){
        System.out.println(\"E\");
        return staticClass;
    }
}

public class Main {
    public static void main(String[] args){
        System.out.println(\"开始\");
        StaticCLass staticClass=StaticCLass.getInstance();
        System.out.println(\"1=\"+StaticCLass.count1);
        System.out.println(\"2=\"+StaticCLass.count2);
        System.out.println(\"3=\"+StaticCLass.count3);
    }
}


````
当建立起类,先在堆内存,进行成员初始化,变量为0/null,如果是有成员是本类是实现,直接是构造方法,然后执行下面的初始化

来源:https://www.cnblogs.com/shuangquantang/p/15984691.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » Java中成员的调用以及static和调用顺序

相关推荐

  • 暂无文章