1Z0-809 · Question #63
Given the following code: ``java public enum USCurrency { PENNY (1), NICKLE (5), DIME (10), QUARTER (25); int value; private USCurrency (int value) { this.value = value; } } public class Coin {…
The correct answer is B. Make the USCurrency enumeration constructor private. C. Remove the new keyword from the instantiation of USCoin. Two compile errors exist in this code: new is incorrectly used to reference an enum constant, and enum constructors must be declared private. Option C removes the illegal new keyword - enum constants like DIME are pre-created singleton instances accessed directly via…
Question
public enum USCurrency {
PENNY (1),
NICKLE (5),
DIME (10),
QUARTER (25);
int value;
private USCurrency (int value) {
this.value = value;
}
}
public class Coin {
public static void main (String[] args) {
USCurrency usCoin = new USCurrency.DIME;
System.out.println(usCoin.getValue());
}
public int getValue() {
return value;
}
}
Which two modifications enable the given code to compile?Options
- ANest the USCurrency enumeration declaration within the Coin class.
- BMake the USCurrency enumeration constructor private.
- CRemove the new keyword from the instantiation of USCoin.
- DMake the getter method of value as a static method.
- EAdd the final keyword in the declaration of value.
How the community answered
(24 responses)- A17% (4)
- B71% (17)
- D4% (1)
- E8% (2)
Explanation
Two compile errors exist in this code: new is incorrectly used to reference an enum constant, and enum constructors must be declared private. Option C removes the illegal new keyword - enum constants like DIME are pre-created singleton instances accessed directly via USCurrency.DIME, not constructed with new. Option B ensures the enum constructor is private, which is required by Java; using public or protected on an enum constructor is a compile-time error.
Why the distractors are wrong:
- A - Nesting
USCurrencyinsideCoinis valid Java but unnecessary; the two-class structure is not the source of the errors. - D - Making
getValue()static doesn't help because the method lives inCoin, notUSCurrency, so calling it on aUSCurrencyreference is still invalid regardless ofstatic. - E - Adding
finalto thevaluefield affects mutability, not compilation; it is unrelated to either error.
Memory tip: Think of enum constants as pre-built objects stored in a catalog - you look them up (USCurrency.DIME), you never build them with new. And because only the enum itself should create its own instances, constructors are always private - no outside construction allowed.
Community Discussion
No community discussion yet for this question.