Wednesday, 29 November 2017

Enum In Java

Enum In Java,Enumeration in Java, Enum,ENumeration,values() method,ordinal() method,Enum vs inheritance,Java.lang.Enum class: , Declaration and usage of enum,Internal implementation of enum,enum vs class,

Introduction :

We can use enum to define a group of named constants.

Example 1: 
enum Month {
 JAN,FEB,MAR,  ... DEC;      //; -->optional
}
Example 2
enum Beer {
 KF,KO,RC,FO;
}
 Enum concept introduced in 1.5 versions.  When compared with old languages enum java's enum is more powerful.  By using enum we can define our own data types which are also come enumerated data types.

Internal implementation of enum :

Image result for java enum

Internally enum's are implemented by using class concept. Every enum constant is a reference variable to that enum type object.  Every enum constant is implicitly public static final always.

Declaration and usage of enum:

Example 4: 
enum Beer {
 KF,KO,RC,FO;//here semicolon is optional.
}
class Test {
public static void main(String args[]){
 Beer b1=Beer.KF; System.out.println(b1);
 }
}
Output: D:\Enum>java Test KF 

Note:
Every enum constant internally static hence we can access by using "enum name".  Internally inside every enum toString() method is implemented to return name of the constant.

If we declare enum inside a class then the allowed modifiers are : 
1.       public      
2.        private default 
3.        protected 
4.       strictfp     
5.        static

Enum vs inheritance:

Every enum in java is the direct child class of java.lang.Enum class hence it is not possible to extends any other enum.  Every enum is implicitly final hence we can't create child enum.
Because of above reasons we can conclude inheritance concept is not applicable for enum's explicitly. Hence we can't apply extends keyword for enum's . But enum can implement any no. Of interfaces simultaneously.

Java.lang.Enum class: 

Every enum in java is the direct child class of java.lang.Enum class. Hence this class acts as base class for all java enums.  It is abstract class and it is direct child class of "Object class" . It implements Serializable and Comparable. 

1. values() method:
Every enum implicitly contains a static values() method to list all constants of enum.  Example: Beer[] b=Beer.values(); 

2. ordinal() method:
Within enum the order of constants is important we can specify by its ordinal value. We can find ordinal value(index value) of enum constant by using ordinal() method.  Example: public final int ordinal(); 

Example
enum Beer {
 KF,KO,RC,FO;
}
 class Test{
public static void main(String args[]){
 Beer[] b=Beer.values();
for(Beer b1:b)//this is forEach loop.
 {
 System.out.println(b1+"......."+b1.ordinal());
}
}
}
 Output:
D:\Enum>java Test
 KF.......0
 KO.......1
RC.......2
FO.......3

DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment