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

JavaScript

When I tried this way

JavaScript

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