Convert String to Enum

P

pluck

Is there a best practise for converting Strings to the new Java 1.5
enums?

This is my current methodology:

public void setAssetClass(String assetClass) throws Exception{
for(AssetClass ac : AssetClass.values()) {
if(assetClass.equals(ac.toString())) {
setAssetClass(ac);
return;
}
}
throw new Exception("Invalid asset class: " + assetClass);
}

private void setAssetClass(AssetClass assetClass) {
this.assetClass = assetClass;
}
 
R

Robert Klemme

Is there a best practise for converting Strings to the new Java 1.5
enums?

This is my current methodology:

public void setAssetClass(String assetClass) throws Exception{
for(AssetClass ac : AssetClass.values()) {
if(assetClass.equals(ac.toString())) {
setAssetClass(ac);
return;
}
}
throw new Exception("Invalid asset class: " + assetClass);
}

private void setAssetClass(AssetClass assetClass) {
this.assetClass = assetClass;
}

I believe you can do

AssetClass.valueOf(str);

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top