Here we will see –
- What is enum in java? What is the use of enum?
- Points related to enum in java.
- Similarities between enum and class.
- Difference between enum and class.
- How to use of enum in real time project.
- Examples of enum in java.
What is enum in java? What is the use of enum?
- Enum is a special data type which contains a list of constants values.
- Enum introduced in jdk 1.5.
public enum Status{ SUCCESS, FAILURE; }
We have a enum with name Status which is having two values SUCCESS and FAILURE. Now come to another point, why we need enum. We can have constants in class something like below –
public class Status{ public static final String SUCCESS = "SUCCESS"; public static final String FAILURE = "FAILURE"; }
Observe both examples. Suppose we have a method which returns either SUCCESS or FAILURE. Since we know our method will return SUCCESS or FAILURE, it is better to define in enum than as traditional constants.
Points related to enum in java.
- Enum is a keyword which introduced in jdk 1.5 which extends java.lang.Enum class.
- Enum can be declared outside or inside a class, but not in a method.
- The semicolon in enum declaration is optional.
- Enum can contain variables, constructors, methods, instance block, static block and constant class body.
- By default enum constructors are private and it can’t be called directly in code. It will be called automatically at the time of enum initialization.
- Whenever we define an enum, it is implicitly final in nature so it can’t extend further.
- We can use enum constants inside a switch statement.
- In case of enum constants == and equals() will behave same.
Let’s see an example for each point.
Enum can be declared outside or inside a class, but not in a method –
//enum can declare outside of class enum Direction { SOUTH, NORTH, EAST, WEST; } public class Test { // enum can declare insdie class enum Direction { SOUTH, NORTH, EAST, WEST; } public void m1() { // we cant define enum inside method /*enum Direction1 { SOUTH, NORTH, EAST, WEST; }*/ } public static void main(String[] args) { System.out.println("Enum can be declared outside or inside a class, but not in a method "); } }
The semicolon in enum declaration is optional –
enum Direction { SOUTH, NORTH, EAST, WEST; } enum Direction1 { SOUTH, NORTH, EAST, WEST }
Both will work fine. But it is always recommended to use semicolon when you are declaring an enum.
Enum can contain variables, constructors, methods, instance block, static block and constant class body –
package enuminjava; enum Direction { SOUTH, NORTH, EAST, WEST; // we can define instance variable inside enum int a = 10; // we can define static variable inside enum static int b = 10; // we can define instance block inside enum { System.out.println("instance block"); } // we can define static block inside enum static { System.out.println("this is a static block"); } // we can define instance method inside enum public void m1() { System.out.println("we can define instance method insdie enum"); } // we can define static method inside enum public static void m2() { System.out.println("we can define static method insdie enum"); } // we can define constructor inside enum Direction() { System.out.println("we can define constructor inside enum"); } // we can define class inside enum class ClassInsideEnum { } // we can define interface inside enum interface i1 { } } public class Test { public static void main(String[] args) { Direction d = Direction.EAST; d.m1(); Direction.m2(); System.out.println(d); } }
Output of above program is –
Observe the output, as we know instance block and constructor both will get executed at object creation, but instance block will execute first then constructor. Also we can see Did you notice that in out static block get executed after instance block and constructor (means in which sequence it has been written), does it behave same in case of class ? Answer is no, it doesn’t matter where you define static block inside class it will get executed at class loading and it will be the first statement in output. Let’s see below example –
In above example we have written static block in the last,but in out put it is the first statement.
[stextbox id=’info’]In case of enum static block will get executed after instance block and constructor. But in case of class static block will always be the first statement.[/stextbox]
By default enum constructors are private and it can’t be called directly in code. It will called automatically at time of enum initialization –
In case of class we can call call constructor explicitly with super and this, but in case of enum constructor will get invoked automatically at time of enum initialization. Always remember private is the only keyword which you can use with enum. Let’s see example –
package enuminjava; enum Direction { SOUTH, NORTH, EAST, WEST; Direction() { System.out.println("we can define constructor inside enum"); } } public class Test { public static void main(String[] args) { Direction d = Direction.EAST; } }
Output of above program –
Observe the output, constructor is getting called four times. We have initialized the enum in main method and for now it has for element, so constructor will get called four times.
Whenever we define an enum, it is implicitly final in nature so it can’t extend further-
We can use enum constants inside the switch statement –
package enuminjava; enum Direction { SOUTH, NORTH, EAST, WEST; } public class Test { public static void main(String[] args) { Direction direction = Direction.SOUTH; switch (direction) { case SOUTH: System.out.println("direction is south"); break; case NORTH: System.out.println("direction is north"); break; case EAST: System.out.println("direction is east"); break; case WEST: System.out.println("direction is west"); default: System.out.println("default season"); break; } } }
Output is – direction is south
Similarities between enum and class.
Let’s see similarities between enum and class –
- Enum and class both can have an instance variable, instance block, instance method, static variable, static block and static method.
- Enum and class both can have constructor.
- Both can have abstract method.
- Both can implements interface.
- Both can have inner class.
Difference between enum and class.
class | enum |
---|---|
By default java.lang.Object is super class for all classes. | By default java.lang.Enum is super type of |
One class can extends another class. | One enum can't extends another enum. |
In case of class you can create object with new keyword. | you can not use new keyword in case of enum. |
If you define static block in enum it will get call at class loading before instance block and constructor. | In case of enum instance block and constructor will get invoked before static block. |
How to use of enum in real time project.
Here we will see how to use in real project, what are the possible scenario where you can use enum.
Example 1 – Suppose we have to define a method where we will perform some task, if the task perform successfully we will return SUCCESS else we will return FAILURE. In this case since we know we have only types of value to return, here we will go for enum. We will define a enum with name Status, which will have two constants SUCCESS and FAILURE. Let’s see a simple example –
enum Status { SUCCESS, FAILURE; } class EnumExample { public Status peformSomeTask() { int a = 10 / 2; if (a == 5) { System.out.println("dummy operation returning success"); return Status.SUCCESS; } else { return Status.FAILURE; } } } public class Test { public static void main(String[] args) { EnumExample enumExample = new EnumExample(); enumExample.peformSomeTask(); } }
Output is – dummy operation returning success
Example 2 – You can define an enum in hibernate inside the entity. Suppose we have a requirement to save some boolean value in a database, which can be true or false. We can define an enum something called TrueFalseIndicator which will have two values T and F and then we can define a property type of TrueFalseIndicator. Let’s see an example –
public enum TrueFalseIndicator { T, F; }
class DummyEntity { @Enumerated(value = EnumType.STRING) @Column(name = "test_indicator") private TrueFalseIndicator testIndicator; }