- How to fix «class, interface, or enum expected» error in Java? Example
- How to Fix Error: “Class, Interface, or Enum Expected” ?
- Misplaced Curly Braces
- Function Outside Class (Intro+ Add Code Example Ti Fix Error)
- Multiple Package
- Conclusion
- How to resolve the «class interface or enum expected» error
- Code
- Case 1 (extra bracket)
- Case 2 (function outside class)
How to fix «class, interface, or enum expected» error in Java? Example
If you have ever written Java programs using Notepad or inside DOS editor, then you know that how a single missing curly brace can blow your program and throw 100s of «illegal start of expression» errors during compilation of Java Programmer. I was one of those lucky people who started their programming on DOS editor, the blue window editor which allows you to write Java programs. I didn’t know about PATH, CLASSPATH, JDK, JVM, or JRE at that point. It’s our lab computer where everything is supposed to work as much as our instructor wants.
Since we don’t have the internet at that point in time, we either wait for the instructor to come and rescue us and we were surprised by how he solve the error by just putting one curly brace and all errors mysteriously go away.
Today, I am going to tell you about one such error, «class, interface, or enum expected». This is another compile-time error in Java that arises due to curly braces. Typically this error occurs when there is an additional curly brace at the end of the program.
Since everything is coded inside a class , interface, or enum in Java, once you close the curly brace for an existing class and add another closing curly braces, the compiler will expect another class is starting hence it will complain about class, interface, or enum keyword as shown in the following program:
public class Main < public static void main(String[] args) < System.out.println("Helloworld"); > > >
If you compile this program using javac, you will get the following error:
$ javac Main.java Main.java:14: class, interface, or enum expected > ^ 1 error
Since this is a small program, you can easily spot the additional curly brace at the end of the problem but it’s very difficult in a big program with several classes and methods.
This becomes even tougher if you are not using any IDE like Eclipse or NetBeans which will give you a visible sign of where an error is. If you know how to use Eclipse or any other Java IDE, just copy-paste your code into IDE and it will tell you the exact location of the error which hint to solve.
Alternatively, if you are coding in Notepad, I assume you are a beginner, then try to correctly indent your code. n our example program above, notice that the two curly braces at the end of the program are at the same indentation level, which cannot happen in a valid program. Therefore, simply delete one of the curly braces for the code to compile, the error will go away as shown below:
So, next time you get the «class, interface, or enum expected» error, just check if you additional curly braces a the end of your program.
- How to fix «variable might not have been initialized» error in Java? (solution)
- Could not create the Java virtual machine Invalid maximum heap size: -Xmx (solution)
- Error: could not open ‘C:\Java\jre8\lib\amd64\jvm.cfg’ (solution)
- How to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory (solution)
- Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger in Java (solution)
- java.lang.OutOfMemoryError: Java heap space : Cause and Solution (steps)
How to Fix Error: “Class, Interface, or Enum Expected” ?
The braces in java are served as a scope limit . The beginning and end of a code block are indicated by curly brackets.
Curly brackets cause the compile-time error known as the class interface or enum expected error in Java. This issue typically happens when the program ends with an extra curly brace.
Since everything in Java is programmed inside a class, interface, or enum, if you close the curly bracket for one class and add another, the compiler will assume that another class is starting and will complain about the class, interface, or enum keyword, as illustrated in the following program:
Code explanation If we compile this code it will raise this error
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.
Code Explanation The following error will be raised
To solve this error simply remove extra brace
Function Outside Class (Intro+ Add Code Example Ti Fix Error)
A method present outside of a class may also be the cause of this problem. The compilation process will fail since this method does not belong to any class. Moving the method within the class will fix this issue.
Code Explanation In Above code method1 is outside of class classb this will raise error.To solve this placed method1 inside of classb.
Multiple Package
In a Java file, only one package can be declared. The expected compilation error for a class, interface, or enum will occur if we have more packages than necessary.
Code Explanation To solve error import only one package.Each source file can only have one package statement, and it must apply to all of the file’s types.
Conclusion
- The class, interface, or enum expected error occurs due to a few different reasons.
- This error can occur due to misplaced braces by removing extra braces or adding missing braces this error can be solved.
- If a method is included outside of a class, then this error can happen. By placing method within class error can be solved.
- This error can be raised if multiple packages are imported in a java file. It is possible to fix errors by importing just one package.
- The solution to this problem is rather simple.
How to resolve the «class interface or enum expected» error
Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.
The class interface or enum expected error is a compile-time error in Java which arises due to curly braces. Typically, this error occurs when there is an additional curly brace at the end of the program.
Let’s try to understand this with an example:
Code
Case 1 (extra bracket)
class Mainpublic static void main(String[] args)System.out.println("Helloworld");>>> //extra bracket.Here, the error can be corrected by simply removing the extra bracket, or by keeping an eye on the indentation.
Case 2 (function outside class)
class MyClasspublic static void main(String args[])//Implementation>>public static void printHello()System.out.println("Hello");>In the above example, we get an error because the method printHello() is outside of the MyClass class. We can fix this by moving the closing curly braces “>” to the end of the file. In other words, move the printHello() method inside of MyClass.
class MyClass < public static void main(String args[]) < //Implementation >public static void printHello() < System.out.println("Hello"); >>