Last active
July 28, 2016 00:46
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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