Web | This Site

Java Basics Examples
Java Tutorial
Java Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# Java Examples - Use of Enum constructor, method

## Problem Description:

How to use enum constructor, instance variable & method?

## Solution:

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

    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("All car prices:");
          for (Car c : Car.values())
          System.out.println(c + " costs "
          + c.getPrice() + " thousand dollars.");
       }
    }

## Result:

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.

Advertisement

Online Image Processing

Indian Baby Names