Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
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.");
Car enumObj =;
System.out.println(enumObj + " costs " + enumObj.getPrice() + " thousand dollars.");
enum Car {
private int price;
Car(int p) {
price = p;
void setPrice(int val){
int getPrice() {
return price;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.