How to Fix "class, interface, or enum expected" Error in Java with Examples?


Every Java programmer whether a beginner or experienced, encounters numerous errors while writing code. Generally, these errors are categorized as run time and compile time errors. The run-time error occurs while running the code after successful compilation and the compile-time error during compilation.

The class, interface, or enum expected is an error thrown during the compilation of source code. It may occur due to several reasons, one of them being misplaced curly braces. In this article, we are going to explore the reasons for this error and corresponding ways to fix the class, interface, or enum expected error.

Fixing class, interface, or enum expected Error in Java

A compilation error indicates that our code does not follow the syntax rules of Java programming language. The class, interface, or enum expected error generated by compiler signifies we have written something in our code that Java compiler does not expect.

The reasons for the class, interface, or enum expected error are:

  • Issue with curly brackets

  • Not declaring a class

  • Defining a method outside a class scope

  • Issue with packages

Let's discuss these issues one by one and our approaches to fixing this error.

Reason 1: Issue with Curly Brackets

As mentioned earlier, the most common reason to encounter the class, interface, or enum expected error is extra or misplaced curly braces. Perhaps, we often see this error due to this reason only, because it is very common for a programmer to miss the braces.

Since we need to put our code inside a class, interface, or enum, whenever we mistakenly add an extra curly brace somewhere in the code. The Java compiler expects a class, interface, or enum.

Example 1

The following example illustrates how we can get an error by misplacing a curly brace.

public class Example1 {
   public static void main(String[] args) {
      int nums = 0;
      nums++; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }
}
} // adding extra curly brace to generate the error

Output

Example1.java:8: error: class, interface, enum, or record expected
} // adding extra curly braces to generate the error
^
1 error

Example 2

The following example illustrates how we can fix the class, interface, or enum error by removing the extra curly brace from the code.

public class Example2 {
   public static void main(String[] args) {
      int nums = 0;
      nums++; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }
}

Output

Incremented value: 1

Reason 2: Not declaring a Class

There may arise a situation when one may forget to define a class and does not enclose the code inside the class at all. In this case, we can get the class, interface, or enum error because according to Java guidelines, every block of code must be defined inside a class. Hence, make sure to wrap every code block within a class.

Reason 3: Defining a Method outside a Class Scope

Another reason that may cause this error is when we mistakenly define a method outside the scope of a class.

Example 3

In the following example, we will deliberately put the 'main()' method outside the class to generate the error.

public class Example3 { }
// from below lines we will get error
   public static void main(String[] args) {
      int nums = 0;
      nums++; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }

Output

Example3.java:3: error: class, interface, enum, or record expected
   public static void main(String[] args) {
                 ^
Example3.java:5: error: class, interface, enum, or record expected
      nums++; // incrementing the value
      ^
Example3.java:6: error: class, interface, enum, or record expected
      System.out.println("Incremented value: " + nums);
      ^
Example3.java:7: error: class, interface, enum, or record expected
   }
   ^
4 errors

Example 4

To fix the previous error, we simply put the 'main()' method within the class as demonstrated in the example.

public class Example4 {
   public static void main(String[] args) {
      int nums = 5;
      nums += 1; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }
}

Output

Incremented value: 6

Reason 4: Issue with packages

When a Java programmer declares multiple packages within a single source code then, we can encounter this problem.

Example 5

In this example, we will declare two packages to generate the error.

package dummy1;
package dummy2;
public class Example5 {
   public static void main(String[] args) {
      int nums = 5;
      nums += 1; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }
}

Output

dummy1/Example5.java:2: error: class, interface, enum, or record expected
package dummy2;
^
1 error

Example 6

In this example, we will remove one of the defined packages to fix the class, interface, or enum expected error.

package dummy1;
public class Example6 {
   public static void main(String[] args) {
      int nums = 5;
      nums += 1; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }
}

Output

Incremented value: 6

Conclusion

In this article, we have understood the class, interface, or enum expected error with a few example programs. Also, we have discovered the reasons for this error and their corresponding approaches to fixing it. The misplaced curly braces is the most common reason of all.

Updated on: 20-Jul-2023

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements