You are here: Home / Topics / Java Program to use values() and valueOf() methods of Enum

Java Program to use values() and valueOf() methods of Enum

Filed under: Java on 2023-09-19 06:52:39

//  Program to use values() and valueOf() methods of Enum.

enum Colour 
{
Red, Green, Blue, Black, White, Orange, Yellow
}

class EnumDemo2 
{
public static void main(String args[ ])
{
 Colour c1;
 
 System.out.println("All Colour constants: ");

 Colour  clr[ ] = Colour.values();
 for(Colour c : clr)
  System.out.println(c + ", ");
 System.out.println();

 c1 = Colour.valueOf("Black");
 System.out.println("c1 contains " + c1);
}
}

 

Output:

All Colour constants: 
Red,
Green,
Blue,
Black,
White,
Orange,
Yellow

c1 contains Black

About Author:
M
Mr. Dubey     View Profile
Founder of MCQ Buddy. I just like to help others. This portal helps students in getting study material free.