Enum concept introduced in JDK 1.5 version.
Java enum more powerful than other language.
The main purpose of enum is if we want to developed or
represent group of constant then we should go for enum.
e.g
enum Friend
{
kunal,sachin,jagdish,atish,vaibhav,vishal;
}
Internally enum
represent as liked this Class concepts
Every enum by default public static final
Every constant
represent as object of type enum
class Friend
{
Public static final Friend sachin=new Friend();
Public static final Friend atish=new Friend();
.
.
.
}
Specialty of java enum:
In old language we represent only constant but in java enum
we declare variable,constructor and method also and enum can have main method
also we run enum from cmd also
i.e java enum is more powerful than other languages;
//simple.java
enum month
{
jan,feb,march,may;
public static void main(String[] args)
{
System.out.println("Hello
World!");
month n=month.jan;
System.out.println(n);
}
}
o/p:
Some valid combination java enum:
- · It can be inside the class.
- · class can be inside enum.
- But inside method does not allowed
enum is direct child class of java.lang.Enum so we cannot
extend any other enum
so that it cannot extend any another enum.
it can implement any number of interface.
One of the most important interview question I faced
No comments:
Post a Comment