Resolving Class Interface or Enum Expected Error

JavaJavaBeginner
Practice Now

Introduction

The class, interface, or enum expected error is a common compile-time error in Java. It occurs when there is a missing curly brace, a method is outside a class, or multiple packages are declared in a single file. In this lab, we will discuss how to resolve this error in Java.

Create a Java File

Create a new Java file named Demo.java in the ~/project directory using the following command:

touch Demo.java

Generate "Class Interface or Enum Expected" Error

Copy and paste the following code in the Demo.java file:

package com.example;
public class Demo {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
public class SomeClass {
    // do something
}

Save the file and run the following command in the terminal:

javac Demo.java

You should see the following error message:

Demo.java:6: error: class, interface, or enum expected
public class SomeClass {
^
1 error

This error indicates that there is an extra class outside of the main class Demo.

Check for Missing Curly Brace

Copy and paste the following code in the Demo.java file:

package com.example;

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

Save the file and run the following command in the terminal:

javac Demo.java

You should see the following error message:

Demo.java:7: error: ';' expected
        System.out.print("The Sum is: " + (a + b));
                                                  ^
1 error

This error indicates that there is a missing curly brace at the end of the main method. To fix the error, add a closing curly brace after the print statement:

package com.example;

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

Save the file and run the following command again:

javac Demo.java

This time the code will compile without any errors.

Correct Multiple Packages Error

Copy and paste the following code in the Demo.java file:

package com.example;

package com.example.test;

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

Save the file and run the following command in the terminal:

javac Demo.java

You should see the following error message:

Demo.java:3: error: class, interface, or enum expected
package com.example.test;
^
1 error

This error indicates that there are multiple packages declared in the same file. You can only declare one package per file in Java. To fix the error, remove the extra package declaration:

package com.example;

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

Save the file and run the following command again:

javac Demo.java

This time the code will compile without any errors.

Summary

The class, interface, or enum expected error occurs due to different reasons, such as a missing curly brace, a method outside a class, or multiple packages in a single file. By following the steps in this lab, you should be able to resolve this error easily. Remember to check the code for any missing curly braces and ensure that there is only one package declaration per file.

Other Java Tutorials you may like