How to Fix Java Error – Class Interface or Enum Expected

Posted by Marta on January 22, 2023 Viewed 3161 times

Card image cap

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.

What’s the meaning of Class, Interface or Enum Expected error?

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.

How to Fix it

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.

Example #1

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;
        }
	}
}

Example #2

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/

Example #3

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");

    }
}

Conclusion

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!

More Interesting Java Tutorials

Project-Based Programming Introduction

Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications

100% Recommended book for Java Beginners

Unique lay-out and teaching programming style helping new concepts stick in your memory

90 Specific Ways to Write Better Python

Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples

Grow Your Java skills as a developer

Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.

Write Code as a Professional Developer

Excellent read for anyone who already know how to program and want to learn Best Practices

Every Developer should read this

Perfect book for anyone transitioning into the mid/mid-senior developer level

Great preparation for interviews

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