Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 11 |
public class MyClass { public static void main(String args[]) { int count=0; } } public static void myMethod() { System.out.println("My method"); } |
You will get below error when you run javac command.
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.
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.