这两个班级是一样的吗?
public class SingleTone {
// yes this is a bug what if i change the class this way
// private static SingleTone instance = new SingleTone();
// and empty constructor?
private static SingleTone instance;
private SingleTone() {
instance = new SingleTone();
}
public static SingleTone getInstance(){
return instance;
}
}
和
public class SingleTone {
private final static SingleTone instance = new SingleTone();
private SingleTone() {
}
public static SingleTone getInstance(){
return instance;
}
}
在构造函数实例化的非最终变量中是否存在任何线程安全问题?
问题2:
有什么区别
private final static SingleTone instance = new SingleTone();
private SingleTone() {
}
还有这个:
private final static SingleTone instance;
private SingleTone() {
instance = new SingleTone();
}