本文主要是介绍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,如果是有成员是本类是实现,直接是构造方法,然后执行下面的初始化
这篇关于Java中成员的调用以及static和调用顺序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!