Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – enum inside another class

The enum can be declared inside another class or interface, but not inside a method.
It is accessed from another class with the name of the class in which it resides.

Example

 class A 
 {
     public enum Fruits 
     {
         Apple, Banana, Pineapple, Papaya, Strawberry
     }
 }

 class B 
 {
     public static void main(String args[]) 
     {
         A.Fruits f[] = A.Fruits.values();
         for(A.Fruits fr:f)
         {
             System.out.println(fr);
         }	
     }
 }

Output

Apple
Banana
Pineapple
Papaya
Strawberry

Inheritance & Enumeration

Every enumeration class extends Enum class directly. Therefore it can’t extend any another enum or a class since multiple inheritance is not allowed in Java.

Every enum is implicitly final. Hence we can’t inherit enum into another enum or class.

 

Leave a reply