(Y.E.S.E.J.) Yet Another Singleton with Enum in Java I have a Java class which has to be generated only once for all the objects that I have - it is a small program. This singleton class holds a mapping of characters. I Googled stack overflow and found that using enum is the most safest way to create a singleton class as Josh Bloch explained in his Java book. 



I also read some reviews that singletons are not good. But for my scope I think I need one. So my code is: 

public enum  SingletonClassA {
    INSTANCE;
    private static Map&lt;Character, Character&gt; instance;
    static {
        Map&lt;Character, Character&gt; aCharMap = new HashMap();
        aCharMap.put('a', 'e');
        aCharMap.put('o', 'u');
        // in order to keep short I erased other puts.
        instance = Collections.unmodifiableMap(aCharMap);
    }

    public static Map&lt;Character, Character&gt; getInstance() {
        return instance;
    }
}




And in order to reach the elements of this HashMap I use 

SingletonClassA.INSTANCE.getInstance().containsKey(aLetter);


Am I creating this singleton class correctly? Do I also need to create a private constructor?

If you have any suggestions about using another data structure for this case please recommend it, write the reasons also.
Despite some other comments, I believe there are times when a singleton is very appropriate, and a powerful tool. The best practice, in Java, since the introduction of enums, is to use them for singletons.

But, your implementation is broken.

When using an enum as a singleton, you should treat the enum as a regular-ish class, and do everything in instance methods, and not have any static content. Then, by declaring just one member in the enum, you get just one instantiation.... always.

So, your code would be better as:

public enum SingletonClassA {
    INSTANCE;

    private final Map&lt;Character, Character&gt; characters;

    // private constructor
    SingletonClassA() {
        Map&lt;Character, Character&gt; aCharMap = new HashMap();
        aCharMap.put('a', 'e');
        aCharMap.put('o', 'u');
        // in order to keep short I erased other puts.
        characters = aCharMap;
        counter++;
    }

    public boolean containsKey(char letter) {
        return characters.containsKey(letter);
    }
}


Your use case then becomes:

SingletonClassA.INSTANCE.containsKey(aLetter);