Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q14 of 15
Java - Static Keyword
Identify the error in the following code snippet:
class Example {
    static {
        System.out.println("Start");
    }
    static void block() {
        System.out.println("Inside method");
    }
}
ANo error, code is valid
BStatic block must have a method name
CStatic block cannot contain print statements
DStatic block cannot be followed by a static method
Step-by-Step Solution
Solution:
  1. Step 1: Check static block rules

    Static blocks can contain code like print statements and do not require a method name.
  2. Step 2: Verify static method declaration

    Static methods can appear after static blocks without any issue.
  3. Final Answer:

    No error, code is valid -> Option A
  4. Quick Check:

    Static blocks and methods can coexist [OK]
Quick Trick: Static blocks can have code; methods are separate [OK]
Common Mistakes:
  • Thinking static blocks need method names
  • Assuming print statements are invalid in static blocks
  • Believing static blocks and methods conflict

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes