Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q7 of 15
Java - Static Keyword
Find the error in this code snippet:
class Test {
  static int count;
  static void increment() {
    count++;
  }
  void display() {
    System.out.println(count);
  }
  static void main(String[] args) {
    increment();
    display();
  }
}
ACannot call static method from instance method
BMissing return type in main method
CCannot call instance method from static method directly
DStatic variables cannot be incremented
Step-by-Step Solution
Solution:
  1. Step 1: Identify method types and calls

    Method 'display' is instance method; 'main' is static method.
  2. Step 2: Check method call from static context

    Static method 'main' cannot call instance method 'display' without an object.
  3. Final Answer:

    Cannot call instance method from static method directly -> Option C
  4. Quick Check:

    Static method can't call instance method without object = D [OK]
Quick Trick: Call instance methods from static methods using object [OK]
Common Mistakes:
  • Trying to call instance method directly from static method
  • Confusing static and instance method rules
  • Assuming static variables can't be incremented

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes