Posted by Marta on January 22, 2023 Viewed 3161 times
In this article you will learn how to fix the java error: class, interface or enum expected and get a better understanding of this error. This will help you to prevent this error in the future.
This compilation error is quite common when you start programming in Java, and it can be really frustrating. In this article, you will learn what “class, interface or enum expected” error means and how to fix it.
The identifier expected error is a compilation error, which means the code doesn’t comply with the syntax rules of the Java language. For instance, one of the java rules is that any piece of code. Not wrapping the code with a class definition means your code won’t compile.
In other words, this error indicates that you wrote a block of code somewhere where java doesn’t expect it.
Here is an code snippet that follows the java syntax rules:
class Hello{ public void printHi(){ System.out.println("HI"); } }
The snippet above contains a class named Hello. Any code you want to write, should go inside the curly bracket, which means the code is part of the class Hello.
Remember in java all functions or methods, should be inside a class.
We have seen what the error actually means, however how could I fix it? The error is “class, interface or enum expected” can come up for many different causes, so depending on the issue you will fix it differently.
As mentioned, the most common reason for this error is that the code is not wrapped with a class definition. Another cause is that there is too many curly brackets, or not enough. See this in action below.
Let’s say you have the following code snippet which returns the error “class, interface or enum expected” :
public static int max(int a, int b){ if(a>b){ return a; }else{ return b; } }
The problem here is that the code is not inside a class, just wrapping the function with a class will fix the problem. Try the code below:
class Operation{ public static int max(int a, int b){ if(a>b){ return a; }else{ return b; } } }
Another code snippet that errors out:
class Hello { public static void main(String[] args) { System.out.println("Helloworld"); } } } // Remove this curly bracket to fix the error
In this case, there is too many curly brackets. In this case, you only need four curly brackets: two to define where the Hello class starts and ends, and another two define the beginning and end of the main method. Therefore you can fix the error by removing the curly bracket at line 10/
Let’s see another example.
class Hello(){ public static void main(String[] args) { System.out.println("Hello"); } }
In this case, the problem is the parenthesis after the class name at line 1. So in java, to define a class the syntax is: the class keyword, then the name of the class, and right after that opening and closing brackets. You can fix the error by deleting the parenthesis. See below:
class Hello{ public static void main(String[] args) { System.out.println("Hello"); } }
To summarise, this article covers how to fix the “class, interface or enum expected” java error. This compilation error will occur when you write code outside a class or the curly brackets are not in pairs.
Hope you enjoy this tutorial and learn what to do when you find the “class, interface or enum expected” error. Thanks for reading and supporting this blog.
Happy coding!
Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications
Unique lay-out and teaching programming style helping new concepts stick in your memory
Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples
Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.
Excellent read for anyone who already know how to program and want to learn Best Practices
Perfect book for anyone transitioning into the mid/mid-senior developer level
Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java