Friday, August 29, 2025
HomeLanguagesJavaHow to Fix “class, interface, or enum expected” Error in Java with...

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

In Java, the class interface or enum expected error is a compile-time error. There can be one of the following reasons we get “class, interface, or enum expected” error in Java: 

Case 1: Extra curly Bracket

Java




class Hello {
  
    public static void main(String[] args)
    {
        System.out.println("Helloworld");
    }
}
} // extra bracket.


 

In this case, the error can be removed by simply removing the extra bracket.

Java




class Hello {
  
    public static void main(String[] args)
    {
        System.out.println("Helloworld");
    }
}


 

Case 2: Function outside the class

Java




class Hello {
    public static void main(String args[])
    {
        System.out.println("HI");
    }
}
public static void func() { System.out.println("Hello"); }


 

In the earlier example, we get an error because the method func() is outside the Hello class. It can be removed by moving the closing curly braces “}” to the end of the file. In other words, move the func() method inside of​ Hello.

Java




class Hello {
    public static void main(String args[])
    {
        System.out.println("HI");
    }
    public static void func()
    {
        System.out.println("Hello");
    }
}


 

Case 3: Forgot to declare class at all

There might be a chance that we forgot to declare class at all. We will get this error. Check if you have declared class, interface, or enum in your java file or not.

Case 4: Declaring more than one package in the same file

Java




package A;
class A {
    void fun1() { System.out.println("Hello"); }
}
package B; //getting class interface or enum expected
public class B {
    public static void main(String[] args)
    {
        System.out.println("HI");
    }
}


 

We can not put different packages into the same source file. In the source file, the package statement should be the first line. 

Java




package A;
class A {
    void fun1() { System.out.println("Hello"); }
}
public class B {
    public static void main(String[] args)
    {
        System.out.println("HI");
    }
}


Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11791 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7012 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS