标签:ret turn track div 运行 oid atom main zab
我们学习java时,通常被告知,变量定义的顺序不重要,可是下面程序确在jdk 1.7上执行出错。
public class FactoryImpl implements Serializable {
private final static FactoryImpl INSTANCE = new FactoryImpl();
private final static AtomicInteger count = new AtomicInteger(0);
private int id;
private FactoryImpl() {
id = count.incrementAndGet();
}
public int getId() {
return id;
}
public static FactoryImpl getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
FactoryImpl impl1 = FactoryImpl.getInstance();
System.out.println(impl1.id);
}
}
把下面代码调整顺序之后能够运行成功。
private static final FactoryImpl INSTANCE = new FactoryImpl();
private final static AtomicInteger count = new AtomicInteger(0);标签:ret turn track div 运行 oid atom main zab
原文地址:http://www.cnblogs.com/tlnshuju/p/7134212.html