How to fix class interface or enum expected error in java

In this post, we will see how to fix "class interface or enum expected" error in java.

There can be multiple reason for getting this error.

Due to method outside class body

This error you will generally get when you have accidentally put your method outside class body.
Let’s see with the help of simple example.

You will get below error when you run javac command.

$javac MyClass.java
MyClass.java:7: error: class, interface, or enum expected
public static void myMethod()
^
MyClass.java:10: error: class, interface, or enum expected
}
^
2 errors

But if you use eclipse ide, you will get below error.

Syntax error on token “}”, delete this token

As you can see, eclipse provides you much better information regarding this error.
I have given a very simple example, you might get this error in complex code. It is hard to identify issues with that so I will recommend you to use some ide like eclipse.

Forgot to declare class at all

This might sound silly but if you forgot to declare class at all, you will get this error.Please check if you have declated class, interface or enum in your java file or not.

That’s all about class interface or enum expected error in java.I hope it will resolve this error for you.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *