0

Simple question : Why is it working like this

class MyCode {
    public static void main(String[] args) {
        System.out.println("This will be printed");
    }
}

but not like this

public class MyCode {
    public static void main(String[] args) {
        System.out.println("This will be printed");
    }
}

giving this error

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at MyCode.main(worksheet.java:2)
iyy0v
  • 51
  • 1
  • 5
  • is ```MyCode ``` outer class or inner class? – fathy elshemy Apr 21 '21 at 09:51
  • 2
    If you define a public class in a java file, the filename must be the same as the class name. – daniu Apr 21 '21 at 09:51
  • 1
    Eclipse should show you a red underline at (or near) the class name. The issue is that public top-level types have to be in files that match their type name. I.e. the public class `MyCode` must be in a file named `MyCode.java`, but yours is in `worksheet.java`. This rule doesn't apply to non-public types, which is why your first sample worked. – Joachim Sauer Apr 21 '21 at 09:51

0 Answers0