nerdexam
Oracle

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…

Java Object-Oriented Approach

Question

  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. System.out.println(name);
  10. }
  11. }
  12. public class App {
  13. public static void main(String[] args) {
  14. Item il = new Item();
  15. il.display("Flower");
  16. }
  17. } 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)
  • B
    5% (1)
  • C
    95% (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 il to item introduces a new error: item is never declared as a variable.
  • C (the stated answer) - Replacing String with lowercase string in the method signature introduces a new compile error; string is not a valid type in Java (String must be capitalized).
  • D - Changing public to private on 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

#static methods#instance variables#scope rules#method overloading

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice