nerdexam
Oracle

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

Given the following code:
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)
  • A
    17% (4)
  • B
    71% (17)
  • D
    4% (1)
  • E
    8% (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 USCurrency inside Coin is 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 in Coin, not USCurrency, so calling it on a USCurrency reference is still invalid regardless of static.
  • E - Adding final to the value field 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.

Full 1Z0-809 Practice