java中单例类的错误

时间:2013-02-02 10:52:08

标签: java singleton

public class SingletonDemo {
    public static void main(String[] args) throws Exception {

        Singleton service = Singleton.getInstance();
        service.doSomething();
    }
}

当我创建单例对象然后获取错误单例无法解析为类型

1 个答案:

答案 0 :(得分:0)

这就是你可以写它的方式

public class SingletonDemo {
    public static void main(String... ignored) {

        Singleton service = Singleton.getInstance();
        service.doSomething();
    }
}

public enum Singleton {
    INSTANCE;

    public static Singleton getInstance() { return INSTANCE; }

    public void doSomething() {
        System.out.println("Singleton.doSomething() called");
    }
}