1Z0-809 · Question #137
Given: ``java public class Test { public static void main(String[] args) { Test test = new Test(); System.out.print(isAvailable + " "); test.isAvailable = true; System.out.println(isAvailable); }…
The correct answer is A. false true. Option A is correct because isAvailable is a static field initialized to false, so the first System.out.print outputs false. The line test.isAvailable = true uses an instance reference to modify a static field - which Java permits - meaning it changes the single class-level…
Question
public class Test {
public static void main(String[] args) {
Test test = new Test();
System.out.print(isAvailable + " ");
test.isAvailable = true;
System.out.println(isAvailable);
}
public static boolean doStuff() {
return isAvailable;
}
static boolean isAvailable = false;
}
What is the result?Options
- Afalse true
- Btrue false
- CCompilation fails.
- Dfalse false
- Etrue true
How the community answered
(29 responses)- A72% (21)
- B3% (1)
- C3% (1)
- D7% (2)
- E14% (4)
Explanation
Option A is correct because isAvailable is a static field initialized to false, so the first System.out.print outputs false. The line test.isAvailable = true uses an instance reference to modify a static field - which Java permits - meaning it changes the single class-level value, not some instance-specific copy. The second System.out.println(isAvailable) then reads that updated static value and prints true, giving the output false true.
Why the distractors fail:
- B (true false): Reverses the order -
isAvailablestartsfalse, nottrue. - C (Compilation fails): The code compiles successfully; accessing a static field via an instance reference (
test.isAvailable) is legal Java, though it generates an IDE warning. - D (false false): Assumes the assignment has no effect, but
test.isAvailable = truegenuinely mutates the static field. - E (true true): Requires
isAvailableto already betrueon the first print, which it isn't - default isfalse.
Memory tip: Think of static as "shared by the whole class." Whenever you see a static field accessed or set through an instance (obj.staticField = x), remember it's just syntactic sugar - it modifies the class-level variable, and every subsequent access (via class or instance) will see the new value.
Community Discussion
No community discussion yet for this question.