Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Error: Class, interface or enum expected

General • Asked over 4 years ago by sanya mehta

sanya mehta Commented on Mar 05, 2021:

Hi, I'm trying to use the IDE in Java, but no matter what code I run, I get multiple errors saying "class, interface, or enum expected" despite the fact that the curly braces check out and that the primary function is within the Main method. Any tips would be appreciated!

Pat Kumar Commented on Jul 04, 2025:

That error usually means the compiler hit something at the top level that isn’t a class/interface/enum—often caused by one missing/extra brace earlier or code sitting outside a class.

Quick checks:
- The file should contain only: optional package, imports, then one or more class/interface/enum declarations. Nothing else.
- Ensure your main is inside a class and spelled exactly: public static void main(String[] args) (lowercase m).
- Public class name must match the filename (e.g., public class Main -> Main.java).
- No statements at top-level (e.g., System.out.println(...) outside any method/class).
- Look for a stray } or an unclosed /* ... */ comment that “hides” a brace. Use your IDE’s auto-format or structure view to see where the class actually ends.
- Make sure there’s no random text or characters before package/import.

Minimal skeleton to compare against:
public class Main { public static void main(String[] args) { /* your code */ } }

If it still blows up, paste the full file (including package/imports). The first line reporting the error is usually near where the class actually ended early.