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