- : -
public enum Currency {PENNY(1), NICKLE(5), DIME(10), QUARTER(25)};
But for this you need to define a member variable and constructor, because PENNY (1) actually calls the constructor, which takes an int value, see the example below.
public enum Currency {
PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
private int value;
private Currency(int value) {
this.value = value;
}
};
The enum constructor in java must be private, any other access modifier will lead to a compilation error. Now, to get the value associated with each coin, you can define the public getValue () method inside java enum, like any normal java class. In addition, no more than half of the colon can be used in the first line.
private int getValue() { return value; }
and get these values: -
PENNY.getValue();
Renouncement:
source
share