@blog.justoneplanet.info

日々勉強

JavaでSingleton

public class Elvis {
    public static final Elvis INSTANCE = new Elvis();
    private Elvis() {}
    private Object readResolve() throws ObjectStreamException {return INSTANCE;}
}
// Elvis.INSTANCE
public class Elvis {
    private static final Elvis INSTANCE = new Elvis();
    private Elvis() {}
    public getInstance() {return INSTANCE;}
    private Object readResolve() throws ObjectStreamException {return INSTANCE;}
}
// Elvis.getInstance();
public enum Elvis {
    INSTANCE;
}
// Elvis.INSTANCE