Java Enum is a Java type used to define collection of constants. A java Enum is special kind of Java class. An enum can contain constants, fields, methods etc. Java Enum was added in Java 5.
Java Enum is the list of constants. enum can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY), transaction type(DEBIT, CREDIT, REFUND). If you use enums instead of integer or String in code, you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.
Basic Enum Example
Days.java
package com.walking.techie; public enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
Notice that enum keyword is used in place of class or interface. The java keyword enum signals to the Java compiler that this type definition is an enum.
EnumTest
package com.walking.techie; public class EnumTest { public static void main(String[] args) { System.out.println(Days.FRIDAY); Days sunday = Days.SUNDAY; System.out.println(sunday); } }
Output of above program is shown below:
FRIDAY SUNDAY
You can refer to the constants in the enum above like this:
Notice that the day variable is of the type Days which is the Java enum type defined in the example above. The day variable can take one of the Days enum constants as value (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, or SATURDAY). In this case day is set to SUNDAY.
Some very important point of java Enum
Point 1.
All enums implicitly extends java.lang.Enum. An enum can not extends anything else because java doesn't support multiple inheritance.
Point 2.
Enum is type-safe. you can not assign anything else other than predefined Enum constants to enum variable. It will give compile-time error if assign something else. It means your enum will have a type for example “Direction” in below example and you can not assign any value other than specified in Enum Constants.
package com.walking.techie; public class Test { public enum Direction { NORTH, SOUTH, EAST, WEST; } public static void main(String[] args) { Direction direction = Direction.EAST; direction = 1; // Compilation error } }
Point 3.
Enum has its own namespace.
Point 4.
Java Enum is like Java class or interface and you can define methods, fields, and constructor etc.
Point 5.
You can specify values of enum constants at the creation time. Direction.values() returns an array of Direction
package com.walking.techie; public class Test { public enum Direction { NORTH(1), SOUTH(2), EAST(3), WEST(4); private int value; Direction(int value) { this.value = value; } } public static void main(String[] args) { for (Direction d : Direction.values()) { System.out.println("Direction name: " + d + " Direction value: " + d.value); } } }
Output of above program is shown below:
Direction name: NORTH Direction value: 1 Direction name: SOUTH Direction value: 2 Direction name: EAST Direction value: 3 Direction name: WEST Direction value: 4
Point 6.
Enum constructor implicitly is private other than private modifier it will give compile time error.
Point 7.
Enum constants implicitly are static and final and can not changed once created.
Point 8.
Enum can be safely compare using:
Point 9.
You can not create instance of enums by using new operator in Java because constructor of Enum in Java can only be private and Enums constants can only be created inside Enums itself.
Point 10.
An instance of Enum in Java is created when any Enum constants are first called or referenced in code.
Point 11.
You can define abstract method inside Enum in java and provide a different implementation for different instances of Enum in java. Let's see an example of abstract method using inside Enum in java.
package com.walking.techie; public enum Direction { NORTH(1) { @Override public String getSymbol() { return "N"; } }, SOUTH(2) { @Override public String getSymbol() { return "S"; } }, EAST(3) { @Override public String getSymbol() { return "E"; } }, WEST(4) { @Override public String getSymbol() { return "W"; } }; private int value; Direction(int value) { this.value = value; } public abstract String getSymbol(); }
Point 12.
Enum in java can implement the interface and override any method like any normal java class. Let's see an example to implement interface using Enum in Java.
package com.walking.techie; public enum Direction implements Runnable { NORTH(1), SOUTH(2), EAST(3), WEST(4); private int value; Direction(int value) { this.value = value; } @Override public void run() { System.out.println("Enum in java implements interface."); } }
When Enum in java implements the interface, you can also give an implementation of a method for each instance of Enum in Java. Let's see an example where Enum in java implements interface, and each instances of it have their own implementation of method.
package com.walking.techie; public enum Direction implements Runnable { NORTH(1) { @Override public void run() { System.out.println("North provides run method implementation."); } }, SOUTH(2) { @Override public void run() { System.out.println("South provides run method implementation."); } }, EAST(3) { @Override public void run() { System.out.println("East provides run method implementation."); } }, WEST(4) { @Override public void run() { System.out.println("West provides run method implementation."); } }; private int value; Direction(int value) { this.value = value; } }
You can call the method of each instance of Enum in java like below.
Point 13.
An enum can be declared outside or inside a class, but not in a method.
Point 14.
An enum declared outside a class must not be marked static, final, abstract, protected, or private.
Point 15.
An enum declared inside a class must not be marked final, abstract.
Point 16.
enum constructors can have arguments, and can be overloaded.
Point 17.
enum constructors can never be invoked directly in code. They always called when an enum is initialized.
Point 18.
An object of enum can not be created using new keyword like any other normal java class can create.
Point 19.
The semicolon at the end of an enum declaration is optional.
These are legal for enum declaration
- enum Direction {NORTH, SOUTH, EAST, WEST}
- enum Direction {NORTH, SOUTH, EAST, WEST};
Simple Java Enum Example
package com.walking.techie; public enum Company { ORACLE("Oracle"), YAHOO("Yahoo"), GOOGLE("Google"), FACEBOOK("Facebook"); private String companyName; Company(String companyName) { this.companyName = companyName; } public String getCompanyName() { return companyName; } }
package com.walking.techie; public class EnumDemo { public static void main(String[] args) { Company company = Company.FACEBOOK; System.out.println("Get Enum value for Company: " + company.getCompanyName()); System.out.println(Company.FACEBOOK.name()); } }
Output of above program is shown below:
Get Enum value for Company: Facebook FACEBOOK
No comments :
Post a Comment