为什么我们需要在静态内部类中将成员声明为静态?我认为在静态内部类中不可能有非静态成员,因此我们不能简单地在不使用静态关键字的情况下声明成员。
class MakeACaptain {
private static MakeACaptain _captain;
private MakeACaptain() {}
//Bill Pugh solution
private static class SingletonHelper {
//Nested class is referenced after getCaptain() is called
private static final MakeACaptain _captain = new MakeACaptain();
}
public static MakeACaptain getCaptain() {
return SingletonHelper._captain;
}
}
这里SingletonHelper是一个静态内部类