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