Enum and Paramterize Enum in Java

Java Enum

What is Enum

Enum is a special data type that consists of a set of pre-defined named values separated by commas. These named values are also known as elements or enumerators or enum instances. Since the values in the enum type are constant, you should always represent them in UPPERCASE letters.

Code Represenation for Enum

public enum Laptop {

    MACBOOK,
    PAVILION,
    SURFACE}

Parameterize Enum

If you want to create enum with values then you need create variable and then also need to create parameterize counstructor

e.g. price is variable which contain values for enum Laptop

public enum Laptop{
MACBOOK(5999),
PAVILION(4999),
SURFACE(5000);

int price;
Laptop(int price){
this.price=price;
}

Enum with Parameterize and Umparameterize values

public enum Laptop{
MACBOOK(5999),
PAVILION(4999),
SURFACE;

int price;
Laptop(int price){
this.price=price;
}

Laptop(){
price=500;
}

Enum with Getter and Setters

Printing values for enum using for loop

public enum Laptop{
MACBOOK(5999),
PAVILION(4999),
SURFACE;

private int price;
Laptop(int price){
this.price=price;
}

Laptop(){
price=500;
}

public int getPrice() {
    return price;
}


public void setPrice(int price) {
    this.price = price;
}
public static void main(String[] args) {

    Laptop lap1= Laptop.Macbook;

    lap1.setPrice(0023);


    for (Laptop lap:Laptop.values()) {
        System.out.println(lap +" : "+lap.getPrice());
    }

}
}
/*Output 
Macbook : 19
HP_Pavilion : 56055
Surface : 799
*/