Enum Contructors - rahul00773/JavaConcepts GitHub Wiki

Enum can contain a constructor. Every time when object of constant will get created constructor will be executed. We can't create even object explicitly by using the new operator. In enum we can't declare abstract methods

Full-Fledged Enum:

package src.enums;

public enum Beer {

    KF(100),KO(70),RC(90),FO;

    int price;
    Beer(int p)
    {
        this.price=p;
    }

    Beer(){
        this.price=65;
    }

    public int getPrice(){

        return price;
    }
    
}

package src.enums;

public class Test3 {

    public static void main(String[] args) {
        
        Beer[] b = Beer.values();

        for(Beer b1: b){

            System.out.println(b1+".........."+b1.getPrice());
        }
    }
    
}