Understanding Enum in Java
Understanding Enum In Java:
Every enum is internally implemented by using Class Concept.
Every enum constant is "public static final" and each constant represents an object of type enum.
enum Month{ class Month{
JAN, FEB; ====> public static final Month JAN=new Month();
} public static final Month FEB=new Month();
}
Enum Declaration & Usage:
We can declare enum inside the class or outside the class but not within the method.
Eg:1 Eg:2 Eg:3
enum X{ class X{ class X{
} enum Y{ public void method(){
} enum Y{
class Y{ } }
} }
}
Compile exception: enum types must not be local
- Enum is a shortcut for Enumeration, it is introduced in 1.5 version of java.
- If we want to represent a group named constants then we should go for Enum.
- The main aim of enum is to define our own data type i.e Enumerated Data Type.
- Java enum is much more powerful than other languages.
Example: enum Week{
MON,TUE,WED,THR,FRI,SAT,SUN;
}
Here ; is optional.
Internal Implementation of Enum:
Every enum is internally implemented by using Class Concept.
Every enum constant is "public static final" and each constant represents an object of type enum.
enum Month{ class Month{
JAN, FEB; ====> public static final Month JAN=new Month();
} public static final Month FEB=new Month();
}
Enum Declaration & Usage:
We can declare enum inside the class or outside the class but not within the method.
Eg:1 Eg:2 Eg:3
enum X{ class X{ class X{
} enum Y{ public void method(){
} enum Y{
class Y{ } }
} }
}
Compile exception: enum types must not be local
Comments
Post a Comment