Easy Tutorial
❮ Exception User File Read Only ❯

Java Example - Enum Constructor and Method Usage

Java Examples

The following example demonstrates the usage of Enum constructors and methods:

Main.java File

enum Car {
    lamborghini(900), tata(2), audi(50), fiat(15), honda(12);
    private int price;
    Car(int p) {
        price = p;
    }
    int getPrice() {
        return price;
    } 
}
public class Main {
    public static void main(String args[]){
        System.out.println("Prices of all cars:");
        for (Car c : Car.values())
        System.out.println(c + " costs " 
        + c.getPrice() + " thousand dollars.");
    }
}

The output of the above code is:

Prices of all cars:
lamborghini costs 900 thousand dollars.
tata costs 2 thousand dollars.
audi costs 50 thousand dollars.
fiat costs 15 thousand dollars.
honda costs 12 thousand dollars.

Java Examples

❮ Exception User File Read Only ❯