HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

(Y.E.S.E.J.) Yet Another Singleton with Enum in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withenumjavayetsingletonanother

Problem

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 instance;
    static {
        Map 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 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.

Solution

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 characters;

    // private constructor
    SingletonClassA() {
        Map 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);

Code Snippets

public enum SingletonClassA {
    INSTANCE;

    private final Map<Character, Character> characters;

    // private constructor
    SingletonClassA() {
        Map<Character, Character> 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);
    }
}
SingletonClassA.INSTANCE.containsKey(aLetter);

Context

StackExchange Code Review Q#106239, answer score: 4

Revisions (0)

No revisions yet.