Take the 2-minute tour ×

I am using an enum singletom pattern like this:

public enum LicenseLoader implements ClientLicense {

     * @return an instance of ClientLicense
    public static ClientLicense getInstance() {
        return (ClientLicense)INSTANCE;

   ...rest of code


Now I want to return the Interface and hide the fact that we are actually using an enum at all. I want the client to use getInstance() and not LicenseLoader.INSTANCE as one day I may decide to use a different pattern if necessary.

Is is possible to make INSTANCE private to the enum?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

What about making a public interface and private enum that implements said interface, with a singleton INSTANCE constant?

So, something like this (all in one class for brevity):

public class PrivateEnum {

    public interface Worker {
        void doSomething();

    static private enum Elvis implements Worker {
        @Override public void doSomething() {
            System.out.println("Thank you! Thank you very much!");

    public Worker getWorker() {
        return Elvis.INSTANCE;

This way, you're not exposing Elvis.INSTANCE (or even enum Elvis at all), using an interface to define your functionality, hiding all implementation details.

share|improve this answer
Good Idea thanks. –  jax Jul 4 '10 at 11:31
This worked perfectly, it allowed me to make the Worker package private also allowing me to get at some stuff I wanted to keep private to the outside world but available inside the package context. –  jax Jul 4 '10 at 11:41
The enum instance is still available, as I explained. –  Tom Hawtin - tackline Jul 5 '10 at 12:48

All enum constants are accessible. For instance through deserialisation or the enum-specific reflective methods.

As always, I strongly suggest avoiding singletons.

share|improve this answer
You are giving bad advice. "The enum singleton pattern provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks." (Effective Java - Joshua Bloch) –  jax Jul 5 '10 at 5:29
No, I'm not giving you bad advice. –  Tom Hawtin - tackline Jul 5 '10 at 12:47
@Tom: what do you mean it's "still available" (comment to my answer)? private accessibility modifier on the enum is not ironclad (e.g. setAccessible attack), but this is true of every class, not just enum. –  polygenelubricants Jul 5 '10 at 12:57
@polygenelubricants You can read the instance with ObjectInputStream.readObject. Also methods such as Enum.valueOf. –  Tom Hawtin - tackline Jul 5 '10 at 13:39
@polygenelubricants Works for me! public class Priv { private enum En { THING } } class Pub { public static void main(String[] args) throws Exception { Class clazz = Class.forName("Priv$En"); System.err.println(Enum.valueOf(clazz, "THING")); } } –  Tom Hawtin - tackline Jul 5 '10 at 14:07

Your Answer


