Skip to content
Advertisement

Class, interface, or enum expected 2 errors [closed]

I wrote this code but got 2 errors like java: class, interface, or enum expected

public class I_Love_Java {

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

When I tried this way

public class I_Love_Java {

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

it works. But I cannot understand what the main problem is.

Advertisement

Answer

All methods in Java must be part of a class (even static methods). In your first example, the main method is not part of any class, whereas in the second example it is.

Advertisement