Skip to content

Instantly share code, notes, and snippets.

@dandrake
Created November 21, 2025 20:29
Show Gist options
  • Select an option

  • Save dandrake/6355e4bb9fab6573252b11694384e6b8 to your computer and use it in GitHub Desktop.

Select an option

Save dandrake/6355e4bb9fab6573252b11694384e6b8 to your computer and use it in GitHub Desktop.
Java enum example -- they are class instances and can have fields/state and behavior/methods
public enum Coffee {
ESPRESSO(2.50, 2),
LATTE(4.50, 8),
CAPPUCCINO(4.00, 8),
AMERICANO(3.00, 8);
// the 'final' isn't necessary functionally, since you can't change these from outside the class/enum
// but this does document our intent, and would prevent accidentally adding something inside this enum
// that modified the field.
private final double price;
private final int ounces;
private Coffee(double price, int ounces) {
this.price = price;
this.ounces = ounces;
}
public double getPrice() { return this.price; }
public int getSize() { return this.ounces; }
public double pricePerOunce() {
return this.price / this.ounces;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment