2

Some guys use Enum instead of a class to create the object and inject it to code. E.g

public enum EnumSample {
    INSTANCE;
    private String name = "Sample Enum";
    private String version = "1";
    public String getName() {
        return this.name;
    }
    
    public String getVersion() {
        return this.version;
    }
}

And in class:

public class MyClass {   
    public static void main(String[] args) {
        new App();
    }
}

public class App {
    private EnumSample enumSample = EnumSample.INSTANCE;
    public App() {
        System.out.printf("%s - %s",enumSample.getName(), enumSample.getVersion());
    }
}

So is it correct? Because in the documentation of Java in Oracle website they said:

From Java documents -

You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.

I just want to know this way is correct or not and is there Any better pattern? Please consider that version is just an example here...

I got that Enum is corret for Singelton ones... I just want to find the other ways to do these without Enum if there is any please share it with me... Thanks

Community
  • 1
  • 1
Ario
  • 1,822
  • 1
  • 21
  • 31

1 Answers1

1

There are actually two main use cases for enum in Java:

(1) Defining a fixed set of constants as mentioned directly in the Java doc.

(2) enum can also be used to safely (thread) create the singleton instance for a class (i.e., without explicitly using synchronize etc..). You can refer here on this subject.

In your EnumSample, you are actually creating one and only one object of the EnumSample which can be referred with the variable INSTANCE (usecase 2 above).

I got that Enum is correct for Singelton ones. I just want to find the other ways to do these without Enum.

In Java, there are several ways to create the singleton instance in a thread-safe manner, one of them is shown below which uses static final INSTANCE with a private constructor:

public class  Sample {

    //Create a static final object      
    private static final Sample INSTANCE = new Sample();

    //private constructor, so this class can't instantiated from outside
    private Sample() {
    }

    //Use the getInstance() static method which returns same instance always
    public static Sample getInstance() {
        return INSTANCE;
    }

    private String name = "Sample Enum";
    private String version = "1";
    public String getName() {
        return this.name;
    }

    public String getVersion() {
        return this.version;
    }
}

public class App {

    public App() {
        System.out.printf("%s - %s",Sample.getInstance().getName(),
          Sample.getInstance().getVersion());
    }
}

You can refer here for the other ways to safely create the singleton.

Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67