Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between a Java enum and a class with private constructor? [duplicate]

I was trying to understand how Java enum really works and I have come to the conclusion that it is very similar to a normal Java class that has its constructor declared private.

I have just come to this conclusion and it isn't based on much thinking, but Id like to know whether I miss anything.

So below is an implementation of a simple Java enum and an equivalent Java class.

public enum Direction {     ENUM_UP(0, -1),     ENUM_DOWN(0, 1),     ENUM_RIGHT(1, 0),     ENUM_LEFT(-1, 0);       private int x;     private int y;      private Direction(int x, int y){         this.x = x;         this.y = y;     }     public int getEnumX(){         return x;     }     public int getEnumY(){         return y;     } } 

What is the difference in meaning between the code above and below?

public class Direction{     public static final Direction UP = new Direction(0, -1) ;     public static final Direction DOWN = new Direction(0, 1) ;     public static final Direction LEFT = new Direction(-1, 0) ;     public static final Direction RIGHT = new Direction(1, 0) ;       private int x ;     private int y ;      private Direction(int x, int y){         this.x = x ;         this.y = y ;     }     public int getX(){         return x;     }     public int getY(){         return y;     } } 
like image 820
user9349193413 Avatar asked Mar 31 '13 20:03

user9349193413


People also ask

What is difference between enum and class in Java?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Can enum have private constructor Java?

Now, you know that Enum can have a constructor in Java that can be used to pass data to Enum constants, just like we passed action here. Though Enum constructor cannot be protected or public, it can either have private or default modifier only.

Can enum have private constructor?

We need the enum constructor to be private because enums define a finite set of values (SMALL, MEDIUM, LARGE). If the constructor was public, people could potentially create more value. (for example, invalid/undeclared values such as ANYSIZE, YOURSIZE, etc.). Enum in Java contains fixed constant values.

What is the difference between enum and enumeration in Java?

enum is a syntactic sugar that allows for meaningful constants; Enumeration is a class that provides way to go through a collection. @Amadan Is Enumeration a class or interface.


2 Answers

Differences:

  1. Enums extend java.lang.Enum and gain all of its nice features:
    1. Automatic singleton behaviour through correct serialization
    2. Automatic human-readable .toString method on enum values without the need to duplicate your enum names
    3. .name and .ordinal special-purpose methods
    4. Usable in high-performance bitset-based EnumSet and EnumMap classes
  2. Enums are treated by the language specially:
    1. Enums use a special syntax which simplifies instance creation without writing dozens of public static final fields
    2. Enums can be used in switch statements
    3. Enums cannot be instantiated outside the enumeration list except by using reflection
    4. Enums cannot be extended outside the enumeration list
  3. Java automatically compiles extra stuff into enums:
    1. public static (Enum)[] values();
    2. public static (Enum) valueOf(java.lang.String);
    3. private static final (Enum)[] $VALUES; (values() returns a clone of this)

Most of these can be emulated with a suitably designed class, but Enum just makes it really easy to create a class with this set of particularly desirable properties.

like image 184
nneonneo Avatar answered Sep 24 '22 15:09

nneonneo


To answer the question: essentially, there's no difference between the two approaches. However, enum construct provides you with some additional supporting methods like values(), valueOf(), etc. which you'd have to write on your own with the class-with-private-constructor approach.

But yeah, I like how Java enums are mostly just like any other classes in Java, they can have fields, behaviors, etc. But to me what separates enums from the plain classes is the idea that enums are classes/types whose instances/members are predetermined. Unlike usual classes where you can create any number of instances from, enums only limit creation to known instances. Yes, as you've illustrated, you can also do this with classes with private constructors, but enums just make this more intuitive.

like image 21
Psycho Punch Avatar answered Sep 24 '22 15:09

Psycho Punch