What is the difference between an enum type and java.lang.Enum?
An enum type, also called enumeration type, is a type whose fields consist of a fixed set of constants. The purpose of using enum type is to enforce type safety.
While java.lang.Enum is an abstract class, it is the common base class of all Java language enumeration types. The definition of Enum is:
public abstract class Enum>
All enum types implicitly extend java.lang.Enum.
The enum is a special reference type, it is not a class by itself, but more like a category of classes that extends from the same base class Enum. Any type declared by the key word "enum" is a different class. They easiest way to declare a enum type is like:
public enum Season {
SPRING, SUMMER, AUTUM, WINTER
}
or
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
Here Season and Day are both enum, but they are different type. All the constants in a enum type is of the same type. For example, SPRING and SUMMER are both of type Season.
Since all enum types All enum types implicitly extend java.lang.Enum, all the methods defined in the Enum class are available to all enum types, such as the
String name() method, which returns the name of this enum constant
int ordinal() method, which returns the ordinal of this enumeration constant, starts from 0
For more information, refer to SCJP Study Guide: Enums
Note: Since Java does not support multiple inheritance, an enum cannot extend anything else.
Most Viewed java Faqs
- How to use HttpURLConnection POST data to web server?(15066)
- What is runtime polymorphism in Java?(9529)
- What is String literal pool?(8821)
- Can the run() method be called directly to start a thread?(8278)
- How to add BASIC Authentication into HttpURLConnection?(7502)
- Can transient variables be declared as 'final' or 'static'?(6323)
- Can static methods be overridden?(4982)
Most Recent java Faqs
- What is the difference between an enum type and java.lang.Enum?
- Which replace function works with regex?
- Why does TreeSet.add throw ClassCastException?
- What is variable hiding and shadowing?
- Can private method be overridden?
- How to enable JDBC tracing?
- How to Retrieve Automatically Generated Keys in JDBC?