甲级 1
public class A {
private static final A instance = new A();
public static A getInstance() {
return new A();
}
}
A级2
public class A {
private static final A instance = new A();
private A(){}
public static A getInstance() {
return instance;
}
}
我刚开始学习单例,我看到了两个使用 A 1 类示例和 A 2 类示例的 Java 示例。A 类 1getInstance()
是单例吗?我也想知道这两个A类方法有什么区别getInstance()
?谢谢