Содержание
- I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:
- 1. Overview
- 2. Misplaced Curly Braces
- 3. Conclusion
I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:
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:
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:
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.
I have been troubleshooting this program for hours, trying several configurations, and have had no luck. It has been written in java, and has 33 errors (lowered from 50 before)
The error log (compiled in JCreator):
I feel like this is a basic error, and yet I can’t seem to find it. If it makes a difference, I am using JCreator to compile and everything is installed correctly.
UPDATE: I have fixed the errors involved (Class declaration and incorrect import statements (someone went back and deleted a few semicolons))
If you have ever written Java programs using Notepad or inside DOS editor, then you know that how a single curly brace can blow your program and throw 100s of error during compilation. I was one of those lucky people who started their programming on DOS editor, the blue window editor which allow you to write Java program. 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 our instructor wants. Since we don’t have the internet at that point of time, we either wait for the instructor to come and rescue us and we surprise 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 which 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 ins >class , interface or enum in Java, once you close the curly brace for an existing class and add another closing curly braces, compiler will expect another class is starting hence it will complain about class, interface, or enum keyword as shown in the following program:
If you compile this program using javac, you will get following 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 error wich 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.
Источник: