adsense


Showing posts with label JAVA EXAMPLES USE OF ENUM CONSTRUCTOR. Show all posts
Showing posts with label JAVA EXAMPLES USE OF ENUM CONSTRUCTOR. Show all posts

Monday, 26 October 2015

JAVA EXAMPLES USE OF ENUM CONSTRUCTOR, METHOD PAGE 84

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES USE OF ENUM CONSTRUCTOR, METHOD


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.