4

In Java we can do the following to initialize class and call method inside that class:

public class MyClass {
  public String myClassMethod() {
    return "MyClass";
  }
}

.

public class Test {
  public static void main(String[] args) {
    MyClass myClass = new MyClass(); // initialize MyClass
    myClass.myClassMethod();// call a method      
  }
}

If my class is an enum class, implementation will be the following:

public enum MyEnumClass {
  INSTANCE;
  public String myEnumClassMethod() {
    return "MyEnumClass";
  }
}

.

public class Test {
  public static void main(String[] args) {
    MyEnumClass myEnumClass = MyEnumClass.INSTANCE;
    myEnumClass.myEnumClassMethod();
  }
}

Both of these cases works in the same way, but it is said to be better in the enum implementation. My question is why and how it is happening?

2
  • 6
    Who says, that an enum implmentation is the "better way"? Classes have their needs, as well as do enums. Commented Nov 14, 2013 at 7:37
  • @Seelenvirtuose refer answers here Commented Nov 14, 2013 at 7:42

2 Answers 2

14

An enum is essentially a singleton pattern.

The JVM handles the initialization and storage of enum instances. To see this most clearly you can write:

public enum MyEnumClass {
    INSTANCE("some value for the string.");

    private final String someString;

    private MyEnumClass(final String someString) {
        this.someString = someString;
    }

    public String getSomeString(){
        return someString;
    }
}

And in another class:

public static void main(String[] args) {
    final MyEnumClass myEnumClass = MyEnumClass.INSTANCE;
    system.out.println(myEnumClass.getSomeString());
}

This would print out "some value for the string.".

This demonstrates that the enum instances are initialised at class load time, i.e. as if by the static initialiser.

Or put another way:

new MyClass() == new MyClass();

Is always false, whereas:

MyEnumClass.INSTANCE == MyEnumClass.INSTANCE;

Is always true. i.e. MyEnumClass.INSTANCE is always the same MyEnumClass.INSTANCE whereas a new MyClass is created every time your call new MyClass().

This brings us nicely to your question of "better".

An enum is a singleton instance with various nifty methods for converting String enum names into a reference to the singleton instance that it represents. It also guarantees that if you de-serialize an enum there won't be two separate instances like there would for a normal class.

So an enum is certainly much better as a robust and threadsafe singleton than a class.

But we cannot have two instances of INSTANCE with the different values for someString so the enum is useless as a class...

In short enums are good for what they're good for and classes are good for what they're good for. They are not substitutes and therefore cannot be compared in any meaningful way expect when one is used as the other.

Sign up to request clarification or add additional context in comments.

2 Comments

No, enum can be used as a singleton pattern, but that's a side effect. Enum is just an enum.
@SilviuBurcea enum can be used as a singleton pattern because it is a singleton, hence it implements the singleton pattern. You can hang some other singleton values off of the enum but that's neither here nor there.
1

It's a simple implementation of the Singleton pattern, relying on the mechanisms of how Enum's work.

If you use MyEnumClass.INSTANCE a second time, you'll get the same object instance.

In contrast, new MyClass(); will create a new object.

See also discussion here:

What is the best approach for using an Enum as a singleton in Java?

There would possibly be more to learn by reading Java Language Spec Section 8-9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.