Enum - rahul00773/JavaConcepts GitHub Wiki

An enum is a group of contents variable.

Example:

enum Month{

Jan, Feb, Mar,... Dec; }

Internally Every enum is implemented by using class objects.

enum Month{

Jan, Feb, Mar,... Dec; }

Equivalent Code:

class Month{

public static final Jan = new Month(); public static final Feb = new Month();

}

We can declare enum in class or outside a class. But we can not declare inside the method and compile-time error will come for enum types must not be local.

If we declare enum outside class applicable modifiers are: public default strictfp

If we declare enum inside class applicable modifiers are: public default strictfp private protected static

From 1.5 version we can pass enum type to switch statement.

package src.enums;

public class Test {

enum Beer{

    FO, KF,MS

}

public static void main(String[] args){

Beer b= Beer.FO;

switch(b){

    case FO:
        System.out.println("KF beer");
        break;

    case KF:
    System.out.println("KF beer");
    break;
default:
    break;
}
}
    
}```


Every Enum in java is a subclass of java.lang.Enum. So every enum can not extends any other enum class.
Every enum is final implicitly that's why we can not create child enum

Enum can't we cant use inheritence. We can not use extends keywords in java


example:

enum x{

}
 
enum y extends x{

}


Example2:

enum x extend java.lang.Enum{

}

example3:

class x {

}

enum y extends x{

}

Example:
enum y{

}

class x extends y{
}



We can use implements keyword with java
interface X{

}

enum X implements X{

}