Java program to fix “class, interface, or enum expected” error with example

 
Case 1: Extra curly Bracket
 
class Hello {

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

In this case, the error can be corrected simply by removing the additional bracket.

class Hello {

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

Case 2: Function outside the class

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

In the previous example, we got an error because the method func() is outside the Hello class. You can delete it by moving the closing brace "}" to the end of the file. In other words, move the func() method to Hello.

 

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

 
Case 4: Declaring more than one package in the same file
 
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");
	}
}
 
package A;
class A {
	void fun1() { System.out.println("Hello"); }
}
public class B {
	public static void main(String[] args)
	{
		System.out.println("HI");
	}
}

 

Submit Your Programming Assignment Details