[Java] 纯文本查看 复制代码
public class Intance01 {
public static void main(String[] args) {
Abs.getInstance();//获取实例
}
}
/**@author tony*/
class Abs{
private static Abs abs = new Abs();
private static int a = 11;
static{
System.out.println("static===="+a+"===");
a = 22;
}
{
System.out.println("初始化===="+a+"===");
a = 33;
}
private Abs(){
System.out.println("构造===="+a+"===");
}
public static Abs getInstance(){
return abs;
}
}
运行结果为:[Java] 纯文本查看 复制代码
* 静态代码、静态变量遵循哪个在前面,哪个先加载个原则.
/** @author tony*/
class Abs{
private static int a = 11;
static{
System.out.println("static===="+a+"===");
a = 22;
}
{
System.out.println("初始化===="+a+"===");
a = 33;
}
private static Abs abs = new Abs();
private Abs(){
System.out.println("构造===="+a+"===");
}
public static Abs getInstance(){
return abs;
}
}