How to Fix “class, interface, or enum expected” Error in Java with Examples?
In Java, the class interface or enum expected error is a compile-time error. There can be one of the following reasons we get “class, interface, or enum expected” error in Java:
Case 1: Extra curly Bracket
In this case, the error can be removed by simply removing the extra bracket.
Case 2: Function outside the class
In the earlier example, we get an error because the method func() is outside the Hello class. It can be removed by moving the closing curly braces “}” to the end of the file. In other words, move the func() method inside of Hello.
Case 3: Forgot to declare class at all
There might be a chance that we forgot to declare class at all. We will get this error. Check if you have declared class, interface, or enum in your java file or not.
Case 4: Declaring more than one package in the same file
We can not put different packages into the same source file. In the source file, the package statement should be the first line.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.