How to use enum constructor, instance variable & method in Java

Problem Description

How to use enum constructor, instance variable & method?


This example initializes enum using a costructor & getPrice() method & display values of enums.

enum Car {
   private int price;
   Car(int p) {
      price = p;
   int getPrice() {
      return price;
public class Main {
   public static void main(String args[]){
      System.out.println("All car prices:");
      for (Car c : Car.values()) System.out.println(
         c + " costs " + c.getPrice() + " thousand dollars.");


The above code sample will produce the following result.

All car prices:
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.