QuestionQ16

Java Object-Oriented Approach

Given:

Question Image

Which change allows the code to compile?

  • A Replace 2 with static String name;
  • B Replace 7 with public static void display(String design) {
  • C Replace 3 with private static void display() {
  • D Replace 15 with Item.display("Flower");
Explanation

A static method cannot directly access an instance field. Declaring name as static makes it a class field, so it can be accessed from the static display() method. The non-static overload still compiles, even though accessing a static field through this is discouraged.

Community Discussion

No comments yet. Be the first to start the discussion!