Signup/Sign In
PUBLISHED ON: AUGUST 24, 2021

Class Interface or Enum Expected Error in Java

The class, interface, or enum expected error is a compile-time error and occurs due to a missing curly brace. This error is pretty easy to resolve. Let's look at some of the cases where this error occurs.

Missing Curly Brace

As discussed above, the class, interface, or enum expected error can occur due to a missing curly brace. The compiler will tell us the exact location of the unwanted curly brace. For example, in the code below, we have an extra closing brace at line 9.

public class Demo
{
	public static void main(String args[])
	{
		int a = 10, b = 15;
		System.out.print("The Sum is: " + (a + b));
	}
}
}

The following image shows the error message that we get when we try to compile the above code. The 9(highlighted) shows the line number where the error occurs. We can rectify it by removing the extra brace.

Error

Method Outside a Class

Another reason for this error could be a method that is present outside a class. For example, in the code below, the someMethod() method is outside the Demo class. This function is not part of any class, and so we will get this error during compilation. We can resolve this error by moving the method inside the class.

public class Demo
{
	public static void main(String args[])
	{
		int a = 10, b = 15;
		System.out.print("The Sum is: " + (a + b));
	}
}
public void someMethod()
{
	//do something
}	

Output:

Error

Multiple Packages

We can only declare a single package inside a Java file. If we have more than more packages, then we will get the class, interface, or enum expected compilation error.

package Package1;
package Package2;
public class Demo
{
	public static void main(String args[])
	{
		int a = 10, b = 15;
		System.out.print("The Sum is: " + (a + b));
	}
}

Error

Summary

The class, interface, or enum expected error occurs due to a few different reasons. It is pretty straightforward to resolve this error. This error is not a big issue if we are using an IDE.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.