Java — In an interface, which explicit declarations are equivalent to the implicit constant defined on line 3? Given: public interface Foo { int k = 4; // Line 3 } Pick the three equivalent explicit forms.

Difficulty: Easy

Correct Answer: 1, 2 and 3

Explanation:


Introduction / Context:
In Java, all fields declared in an interface are implicitly public, static, and final. Recognizing this is fundamental for Java language semantics and avoids redundant modifiers or invalid ones.



Given Data / Assumptions:

  • An interface Foo declares int k = 4;
  • We must choose which explicit forms are equivalent.


Concept / Approach:
Interface fields are constants: public static final. Therefore, explicitly writing any subset of these (that still implies the constant nature) is equivalent in meaning. Invalid modifiers (abstract for fields, volatile for interface constants) are not equivalent.



Step-by-Step Solution:

Option 1 (final int k=4): matches the “final” aspect; still implicitly public static in an interface context.Option 2 (public int k=4): public is explicit; still implicitly static final.Option 3 (static int k=4): static is explicit; still implicitly public final.Option 4 (abstract int ...): abstract is for methods, not fields; invalid.Option 5 (volatile int ...): interface constants are final; volatile cannot apply.


Verification / Alternative check:
Compile each variant inside an interface and observe that 1–3 compile; 4–5 do not or do not match semantics.



Why Other Options Are Wrong:
abstract does not apply to fields; volatile contradicts final and constant semantics.



Common Pitfalls:
Assuming interface fields behave like class fields; they do not—they are constants.



Final Answer:
1, 2 and 3

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion