Java — Identify which class-level (non-local) variable declarations will not compile

Difficulty: Easy

Correct Answer: private synchronized int e;

Explanation:


Introduction / Context:
This question tests knowledge of valid modifiers for class-level (non-local) variables in Java. Some modifiers are allowed for variables, while others are only valid for methods.



Given Data / Assumptions:

  • Scope: class-level variables, not local or method-level.
  • Keywords in play: protected, transient, synchronized, volatile.


Concept / Approach:
For variables:

  • Access modifiers (public, protected, private) are valid.
  • transient and volatile are valid modifiers for variables.
  • synchronized is not valid for variables; it only applies to methods or blocks.


Step-by-Step Reasoning:

protected int a; → Valid.transient int b = 3; → Valid for serialization behavior.private synchronized int e; → Invalid because synchronized cannot be applied to variables.volatile int d; → Valid, ensures visibility across threads.


Why Other Options Are Wrong:
All except synchronized are legal modifiers for variables.



Common Pitfalls:
Confusing synchronized with volatile; synchronized is only for methods/blocks, not variables.



Final Answer:
private synchronized int e;

Discussion & Comments

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