Is there any difference between this declaration
Thread.State state = Thread.State.NEW;
and that
Enum<Thread.State> state = Thread.State.NEW;
in Java? Instead of the second option is a bit longer?
An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).
They are type safe and comparing them is faster than comparing Strings. Save this answer. Show activity on this post. If your set of parameters is limited and known at compile time, use enum .
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
Enums have their own data type and each enum is essentially a new data type. This is where one of their their biggest powers lie - they can be strongly type checked at compile time. I have added further values to demonstrate the resultant versatility.
It's the same case as comparing between:
Child o = someChild;
and
Parent o = someChild;
Enum
is the parent class of all enum types. Therefore, with the second line, the code cannot contain references to specific members of Thread.State
, specifically the members described in this section of the language spec.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With