enum is the common base class of all Java language enumeration types. including descriptions of the implicitly declared methods synthesized by the compiler
valueof method returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type
valueof method returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type
Method syntax
public static > T valueOf(Class enumType, String name)
Method input parameter
enumType - the Class object of the enum type from which to return a constant
name - the name of the constant to return
name - the name of the constant to return
Method return
the enum constant of the specified enum type with the specified name
Example Code for java enum valueof
enum Car {
City(800000), Civic(1200000),Jaz(850000);
int price;
Car(int p) {
price = p;
}
int showPrice() {
return price;
}
}
public class EnumTest {
public static void main(String args[]) {
System.out.println("My Car List:");
for(Car m : Car.values()) {
System.out.println(m + " Price " + m.showPrice());
}
Car city;
city = Car.valueOf("City");
System.out.println("Test Selected Car : " + city);
}
}
Runnint Result
My Car List:
City Price 800000
Civic Price 1200000
Jaz Price 850000
Test Selected Car : City
City Price 800000
Civic Price 1200000
Jaz Price 850000
Test Selected Car : City