You are here: Home / Topics / Program to use Java Enum as a Class type in Java

Program to use Java Enum as a Class type in Java

Filed under: Java on 2023-09-19 06:55:19

//  Program to use Java Enum as a Class type.

enum Colour 
{
Red(200), Green(150), Blue(100), Black(250), White(175), Orange(225), Yellow(125);

private int price;   // price of each colour

Colour(int p) 

 price = p; 
}

int getPrice() 

 return price; 
}
}

class EnumDemo3
{
public static void main(String args[ ])
{
 Colour c1;
 
 // Display price of Green.
 System.out.println("Green costs " + Colour.Green.getPrice() + " Rs.");

 // Display all colours and prices.
 System.out.println("All Colour's prices: ");
 for(Colour c : Colour.values())
  System.out.println(c + " costs " + c.getPrice() + " Rs.");
}
}


Output:

Green costs 150 Rs.
All Colour's prices: 
Red costs 200 Rs.
Green costs 150 Rs.
Blue costs 100 Rs.
Black costs 250 Rs.
White costs 175 Rs.
Orange costs 225 Rs.
Yellow costs 125 Rs.

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.