Reputation: 85
I have an enum Class Property
and in some part of my programm I want to replace a String
or Serializable
value in my Property
. How can i do that?
public enum Property {
Autofocus,
Bluetooth,
Brand,...}
How can i assign value to Property
using a Serializable
value or a String
:
Property property;
this.property = (Serializable) value;
or
this.property = "value";
instead of
this.property = Property.Autofocus;
this.property = Property.Brand; // ...
Upvotes: 2
Views: 774
Reputation: 11597
Property.valueOf("Bluetooth");
will take the value from string to the value of the enum if that's what you meant
The static methods valueOf()
and values()
are created at compile time and do not appear in source code. They do appear in Javadoc, though; for example, Dialog.ModalityType
shows both methods.
Upvotes: 1
Reputation: 26084
Use java.lang.Enum.valueOf(Class<T> enumType, String name)
:
Property p = Enum.valueOf(Property.class, "Autofocus");
System.out.println(p);
Upvotes: 2
Reputation: 425288
Every enum
has an implicit method valueOf(String)
that returns the enum instance with the specified name:
Property p = Property.valueOf("Autofocus");
Note that this method throws an IllegalArgumentException
for unknown values.
Upvotes: 2