Skip to content

Instantly share code, notes, and snippets.

@ishanbakshi
Last active July 28, 2016 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishanbakshi/4b78786b244ed9a6dce7a94ddb18c2d8 to your computer and use it in GitHub Desktop.
Save ishanbakshi/4b78786b244ed9a6dce7a94ddb18c2d8 to your computer and use it in GitHub Desktop.
This gist demonstrates, how to set/replace default enum properties at runtime.
// this code was tested using java ide from tutorials point
// see the running code on http://goo.gl/nhXzCc
public class EnumWithPropertiesExample{
public static void main(String []args){
System.out.println("All car prices:");
for (Car c : Car.values()){
System.out.println(c + " costs " + c.getPrice() + " thousand dollars.");
}
System.out.println("--------");
Car enumObj = Car.lamborghini;
enumObj.setPrice(21);
System.out.println(enumObj + " costs " + enumObj.getPrice() + " thousand dollars.");
}
}
enum Car {
lamborghini(800),tata(2),audi(50),fiat(15),honda(12);
private int price;
Car(int p) {
price = p;
}
void setPrice(int val){
this.price=val;
}
int getPrice() {
return price;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment