1. Overview

In this quick tutorial, we’re going to talk about the Java compiler error “class, interface, or enum expected”. This error is mainly faced by developers who are new to the java world.

Let’s walk through a few examples of this error and discuss how to fix them.

2. Misplaced Curly Braces

The root cause of the “class, interface, or enum expected” error is typically a misplaced curly brace “}”. This can be an extra curly brace after the class. It could also be a method accidentally written outside the class.

Let’s look at an example:

public class MyClass {
    public static void main(String args[]) {
      System.out.println("Baeldung");
    }
}
}
/MyClass.java:6: error: class, interface, or enum expected
}
^
1 error

In the above code example, there is an extra “}” curly brace in the last line which results in a compilation error. If we remove it, then the code will compile.

Let’s look at another scenario where this error occurs:

public class MyClass {
    public static void main(String args[]) {
        //Implementation
    }
}
public static void printHello() {
    System.out.println("Hello");
}
/MyClass.java:6: error: class, interface, or enum expected
public static void printHello()
^
/MyClass.java:8: error: class, interface, or enum expected
}
^
2 errors

In the above example, we’ll get the error because the method printHello()  is outside of the class MyClass. We can fix this by moving the closing curly braces “}”   to the end of the file. In other words, move the printHello() method inside MyClass.

3. Conclusion

In this brief tutorial, we have discussed the “class, interface, or enum expected” Java compiler error and demonstrated two likely root causes.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.