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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/inner_classes("`Inner Classes`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/inner_classes -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/classes_objects -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/class_methods -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/modifiers -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/oop -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/identifier -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/arrays -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/comments -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/data_types -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/operators -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/output -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/strings -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/variables -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} java/system_methods -.-> lab-117393{{"`Resolving Class Interface or Enum Expected Error`"}} end

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