Week 98 — What is an enum?
Question of the Week #98
What is an enum?
7 Replies
The
Object
class provides an equals(Object)
method that can be used to test for equality. To do that, one can override that method and write code for equality checks.
If two objects are considered equal, firstObject.equals(secondObject)
should return true
, else it should return false
.
When overriding equals
, one should make sure that a.equals(b)
gives the same result as b.equals(a)
(assuming that neither a
nor b
are null
) and always also override hashCode
and make sure that the hashCode()
result is the same for all objects a
and b
where a.equals(b)
is true.
The methods equals(Object)
and hashCode()
are used by many classes in the JDK for comparing objects, e.g. HashMap
.Sometimes, it's also necessary to define some sort of order between objects. This can be done using the
Comparable
interface.
A class can mark itself to be comparable to another class by implementing that interface and overriding the compareTo
method.
If firstObject
and secondObject
are equal (by the equals(Object)
method), firstObject.compareTo(secondObject)
as well as secondObject.compareTo(firstObject)
should return 0
.
If firstObject
is considered to be greater than secondObject
, then firstObject.compareTo(secondObject)
should return a value greater than 0
and secondObject.compareTo(firstObject)
should return a value smaller than 0
.
📖 Sample answer from dan1st
Enums are classes where all instances of the class are known at compile-time. An enum can be declared using the
enum
keyword with a list of all enum constants (list of the objects of the enum):
After an enum is declared, its instances can be obtained like regular constants and used like other objects:
All enums have an ordinal()
method that can be used to get the index of the enum value in the declaration:
Similarly, the static
method called values
(which is present in every enum) returns an array of all values of that enum in the order of declaration. The value returned by ordinal()
is the the index of that enum value in the array:
Apart from that, there is an implicit static
method named valueOf
in each enum class that can be used to get an enum value by name:
Enums always extend java.lang.Enum
and cannot have any subclasses.Just like classes, enums can have fields and constructors but the constructors cannot be called except using the enum constants but a constructor must be called in the enum declaration:
📖 Sample answer from dan1st
An Enum is a Type of document, like a class or an interface.
In this document you can write a number of values that can be use in the code.
A code example can be:
public enum numbers {
ONE,
TWO,
THREE
}
And then you can call de enum numbers in youre code and be sure that the only numbers you can use are ONE, TWO and THREE thanks to this enum....
A practical example could be putting the extensions of files that your code accepts...
Submission from arielb0591
An enum is a special kind of Java class with a fixed set of values. They are similar in function to a regular class with a private constructor and a fixed number of
public static final
instances. However, enums have some advantages.
As an example, compare the following enum....
.... with the following regular class....
First, the enum is more compact, requiring less code to achieve a similar result. The enum's constants must be the first declarations in the class. The last one can be terminated with a semicolon, and then additional members -- including constructors, fields, and methods -- can be added to the enum, just like with any class. The only restriction is that constructors must be private; if the private
access modifier is omitted, it will still be implied.
Second, enum classes implicitly extend an Enum
base class, which provides methods for retrieving the enum value's name and ordinal. The Enum
class also implements Comparable
and Serializable
, so all values of an enum can be compared, sorted, and serialized. Enum
also exposes a static method to lookup any enum value by class and name. In addition to that, the compiler will generate a similar static method on each enum. To illustrate that last point, these two method calls are equivalent:
Finally, the JVM will ensure that no additional instances of the class exist beyond the declared values. In the "regular" DaysOfTheWeek
class above, serialization can be used to circumvent the visibility restrictions on the constructor, which would result in instances that are not one of the 7 static final values. However, in the enum class, the JVM treats enum deserialization specially to ensure that the existing instances are returned.
Note that the JVM/JDK also blocks most reflective mechanisms from creating new enum instances. Be warned, though, that there are a couple of arcane mechanisms (notably, the soon-to-be-retired Unsafe
class) that can actually break the normal guarantees around enum instances. These should never be used.
Because of these guarantees, enum instances can be safely compared with
==
instead of .equals()
. They can also be used in switch
statements, just like integer values. As of Java 14, they can also be used in switch expressions. Because the number of values is fixed and known at compile time, the cases can be exhaustive, meaning a default
block is not required.⭐ Submission from dangerously_casual