1Z0-829 · Question #24
1. class Item { 2. String name; 3. public static void display() { 4. name = "Vase"; 5. System.out.println(name); 6. } 7. public void display(String design) { 8. this.name += name; 9…
The correct answer is C. Replace 7 with public void display (string design) {. There is an issue with the stated correct answer here that's important to flag for your exam preparation. The actual compile error is in the static method display() at lines 3-6: it accesses the instance variable name directly (name = "Vase"), which is illegal in Java - static…
Question
- class Item {
- String name;
- public static void display() {
- name = "Vase";
- System.out.println(name);
- }
- public void display(String design) {
- this.name += name;
- System.out.println(name);
- }
- }
- public class App {
- public static void main(String[] args) {
- Item il = new Item();
- il.display("Flower");
- }
- } Which action enables the code to compile?
Options
- AReplace 15 with item.display("Flower");
- BReplace 2 with static string name;
- CReplace 7 with public void display (string design) {
- DReplace 3 with private static void display ( ) {
How the community answered
(20 responses)- B5% (1)
- C95% (19)
Explanation
There is an issue with the stated correct answer here that's important to flag for your exam preparation.
The actual compile error is in the static method display() at lines 3-6: it accesses the instance variable name directly (name = "Vase"), which is illegal in Java - static methods cannot reference non-static fields without an object instance.
Option B is the real fix. Making name static (static String name;) allows the static method to access it directly, resolving the compile error. All other methods (instance or static) can still access static fields, so nothing else breaks.
Why each distractor fails:
- A - Changing
iltoitemintroduces a new error:itemis never declared as a variable. - C (the stated answer) - Replacing
Stringwith lowercasestringin the method signature introduces a new compile error;stringis not a valid type in Java (Stringmust be capitalized). - D - Changing
publictoprivateon the static method only changes visibility, not the static/non-static field access issue.
Note for exam takers: This question appears to have an error in the published answer key. If you see it on an exam, B is the technically correct fix. If forced to choose C, be aware that lowercase string is valid in C# but not Java - a common confusion in multi-language exam contexts.
Memory tip: "Static methods live outside any object - they can only see static things." When a static method touches an instance variable, make the variable static or pass an object reference in.
Topics
Community Discussion
No community discussion yet for this question.