Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumeration or Class?

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?

like image 451
Evgenij Reznik Avatar asked Jul 27 '26 17:07

Evgenij Reznik


2 Answers

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.

  • if you want to highlight that it's this thing that can instantiate an object with state and methods, call it a class
  • if you want to highlight the object itself, call it the singleton instance
  • if you want to highlight that it's using the enum to implement the singleton pattern, call it an enum
  • if you want to highlight the fact that it's a singleton (without referring to how that pattern is implemented), call it a singleton
  • if you want to highlight the fact that it's a singleton pattern implemented via an enum, call it a singleton enum

I would understand any of those terms, and I wouldn't judge someone for using one vs another.

like image 165
yshavit Avatar answered Jul 30 '26 05:07

yshavit


It's an enum, and it's also a class. Enum is a type of class.

like image 29
MByD Avatar answered Jul 30 '26 07:07

MByD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!