1Z0-819 · Question #6
Analyze the code: public class Test { private static String prefix = "Global:"; public static String name = "Namespace"; public static String getName() { return new Test().name; } public static void…
The correct answer is B. Test.prefix+Test.name() C. Test.prefix+Test.getName(). C is definitionally correct: Test.prefix accesses the private static field "Global:" from within the same class (which is always valid - private only blocks outside classes, not methods within the declaring class itself), and Test.getName() is a proper call to the public static…
Question
Options
- ATest.prefix+Test.name
- BTest.prefix+Test.name()
- CTest.prefix+Test.getName()
- DTest.prefix+name
- Eprefix+Test.name
- Fprefix+name
How the community answered
(52 responses)- A2% (1)
- B81% (42)
- D6% (3)
- E10% (5)
- F2% (1)
Explanation
C is definitionally correct: Test.prefix accesses the private static field "Global:" from within the same class (which is always valid - private only blocks outside classes, not methods within the declaring class itself), and Test.getName() is a proper call to the public static method that returns "Namespace", yielding "Global:Namespace".
B is actually a compile error, not a valid answer: name is a String field, not a method - Test.name() is invalid because no method called name() exists in Test. The stated answer key appears to contain an error here.
The distractors that seem wrong but actually work: Options A (Test.prefix+Test.name), D (Test.prefix+name), E (prefix+Test.name), and F (prefix+name) would all compile and produce the correct output, because main lives inside Test and can freely access prefix in any form. This makes the question poorly constructed - there are actually five valid answers, not two.
What the question likely intended to test: The distinction between accessing a field (name) vs. calling a method (getName()), and understanding that private members are accessible within the declaring class. The most defensible correct pair is probably A and C, or C and F.
Memory tip: In Java, private = "only this class can see it" (not "only this method"). Any code inside Test - static or not - can read Test.prefix freely.
Topics
Community Discussion
No community discussion yet for this question.