Reputation: 18594
I implement a Singleton using the enum approach:
public enum Singleton {
INSTANCE;
public void doStuff(String stuff) {
System.out.println("Doing " + stuff);
}
}
How do I call it right (I don't mean execute, just how to write it)? Is it still a class or now an enumeration? I'm just interested in the theoretical part.
If talking about object orientation and classes, can I say I created a Singleton class of type enumeration?
Upvotes: 3
Views: 218
Reputation: 8530
It is Enum and it is also a type of class since Java5.0.
You are creating Singleton with Enum.
learn more about Enum
Upvotes: 0
Reputation: 43391
An enum in Java is just a class (as you seem to know), so you should be fine calling it any of the above. It's a bit subjective, but I think it depends on which aspects of it you want to highlight.
I would understand any of those terms, and I wouldn't judge someone for using one vs another.
Upvotes: 3
Reputation: 1793
It's an enum. The java language ensures the INSTANCE enum value will be instantiated once (per classloader, so usually once per JVM) and the Singleton.INSTANCE field will let you access the single instance.
You could also just do:
public class Singleton {
public static Singleton instance INSTANCE = new Singleton();
private Singleton() { /* private ctor so people can't instantiate their own */ }
}
but the enum approach makes it cleaner (if you have JDK5 or up)
Upvotes: 0