What if main method is inside “non public class” of java file?

Actually you can execute the main method in a non-public class. if you put this class

class A {
  public static void main(String... args) {
      System.out.println("This is not a public class!");
  }
}

in a file named NonPubClass.java. You can compile this file using javac command but you will not get a NonPubClass.class, you will get a A.class instead. Use java a to invoke that class and you will see the printed string — This is not a public class!

Leave a Comment