Quick Answer
Fix misplaced code outside type declarations.
Understanding the Issue
This syntax error occurs when code exists outside class/interface/enum declarations. Java requires all executable statements to be inside type definitions.
The Problem
This code demonstrates the issue:
Java
Error
System.out.println("Hello"); // Top-level code
public class Test {}
The Solution
Here's the corrected code:
Java
Fixed
// Correct structure
public class Test {
public static void main(String[] args) {
System.out.println("Hello"); // Inside class
}
}
// Alternative: Use static initializer
public class Test {
static {
System.out.println("Hello");
}
}
Key Takeaways
All executable code must be within type declarations.