Home » Core Java » Java Enum

Java Enum

Java Enum Overview ::

1. In java an enum type is a special data type which enables a variable to hold a set of predefined constants.It means basically enum  is a datatype which can hold a set of constants.
2. Directions(EAST,WEST,NORTH,SOUTH) is an example of enum or DAY(SUN,MON,TUE,WED,THU,FRI,SAT) is  an example of enum.
3. enum can be traversed using values() method.
4. enum can have constructor,fields and also user defined methods.
5. enum internally extends enum class ,so it can not extend any other class .enum can implement multiple interfaces.

6. enum can be used in switch statements,enum is final so we can use for Singleton,enum ensures type safety as well.These are some of the reasons why we use enum.

7. You can use valueOf(String arg) to get a particular enum and you can use valueOf(Class<T>  arg, String arg1) to get a particular enum of a class Type.
8. enum was introduced in Java 5 version jdk 1.5

You can directly create enum java file by following the below image.

Let us create an enum called Apartment.

Output ::

Leave a comment

Your email address will not be published. Required fields are marked *

two × 3 =