Home › Java

error: class, interface, enum, or record expected (Java) — how to fix

Main.java:12: error: class, interface, enum, or record expected
}
^
1 error

Quick fix: Go to the line in the error and count braces: remove the extra } or move the method/statement back inside the class body. Every method, field and statement must be inside public class Name { ... }. An IDE's auto-format (Ctrl+Alt+L in IntelliJ, Shift+Alt+F in VS Code) makes mismatched braces obvious.

What it means

At the top level of a .java file, the compiler only accepts package, import and type declarations (class, interface, enum, record). Anything else there (a method, a field, a statement, a stray }) produces this error. JDK 16+ includes "record" in the message; older versions say class, interface, or enum expected.

The line number points at the first thing found outside a type. The real mistake is usually just above it.

Cause 1: an extra closing brace

public class Main {
    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.println(args[0]);
        }
        }        // <- closes main
    }            // <- closes the class early
    static void helper() { }   // now outside the class -> error
}

Fix: remove the extra }. Reformatting the file shows the problem quickly, because the indentation will no longer match what you expected. In IntelliJ use Code > Reformat Code; in VS Code, Format Document. Most editors also highlight the matching brace when the cursor is on one.

Cause 2: a method written after the class

public class Main {
    public static void main(String[] args) {
        greet();
    }
}

static void greet() {          // error: outside Main
    System.out.println("hi");
}

Fix: move it inside:

public class Main {
    public static void main(String[] args) {
        greet();
    }

    static void greet() {
        System.out.println("hi");
    }
}

Cause 3: statements or variables at file level

Java (unlike Python or JavaScript) does not allow code outside a class:

import java.util.*;

int count = 0;                 // error
System.out.println("start");   // error

public class Main { ... }

Put fields in the class and statements inside a method such as main.

Note: newer JDKs have a preview feature for implicitly declared classes that allows top-level methods. Unless you enabled preview features, write a normal class.

Cause 4: a typo in the declaration keyword

Class Main, pubic class Main, or public Main { (missing class) all make the compiler fail to recognise a type declaration, so it reports this error at that line. Check the spelling and case: Java keywords are lowercase.

Cause 5: pasted code or a missing opening brace

When code is pasted from a tutorial, an opening { is sometimes lost, for example public static void main(String[] args) with no {. The compiler then misreads the following lines and may report several errors. Fix the first error first and recompile: later errors often disappear.

Cause 6: wrong file contents

A file that contains text that isn't Java (merge conflict markers <<<<<<<, a copied stack trace, a BOM in some old toolchains) can trigger it too. Search the file for <<<<<<< and >>>>>>> after a merge.

Last updated 2026-11-29.