Bird
0
0

Identify the error in this code:

medium📝 Debug Q14 of 15
Java - Static Keyword
Identify the error in this code:
class Demo {
    static void show() {
        System.out.println("Hello");
    }
    public static void main(String[] args) {
        show();
        Demo d = new Demo();
        d.show();
    }
}
ACannot call static method using object reference
BMissing return type in show method
CNo error, code runs fine
DStatic method cannot access System.out
Step-by-Step Solution
Solution:
  1. Step 1: Check static method calls

    Static methods can be called using class name or object reference. Both show() and d.show() are valid.
  2. Step 2: Verify method declaration and usage

    The method show() has correct return type void and uses System.out.println correctly.
  3. Final Answer:

    No error, code runs fine -> Option C
  4. Quick Check:

    Static methods callable via object or class = B [OK]
Quick Trick: Static methods can be called via object or class name [OK]
Common Mistakes:
  • Thinking static methods can't be called on objects
  • Assuming missing return type error
  • Believing static can't use System.out

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes