Compiler error: "class, interface, or enum expected"
In Java sometimes we get an error as when we creating a new class in the project or modifying the existing class content.
There are many cases when we get this error
1) Misplaced Curly Braces
public static void main(String args[]) { System.out.println("Hello World!!"); } } Test.java:6: error: class, interface, or enum expected } ^ 1 error
public class Test {
there is missing "}" curly brace in the last line which results in a compilation error. If we remove it, then the code will compile.
2) A missing class declaration
import java.util.Random;
public static void addCustomer(String args[])
{
// a bunch of code
}
so here we missed the class declaration.
public class Customer{
public static void addCustomer(String args[]){ ... }
}
3) Method defined outside the class
public static void main(String args[]) { //a bunch of code } } public static void display() { System.out.println("Hello World!!"); }
public class Test {
Every method should be within a class. Your method display() is outside a class.
public class ClassName {
///your methods
}
If we put the method inside the class, then the code will compile.
public static void main(String args[]) { public static void display() { System.out.println("Hello World!!"); } } }
public class Test {
That's it!! We have seen why the error "the class, interface, or enum expected" occurred while writing code in java and seen the possible solutions to fix it.
0 Comments
Post a Comment