Java compilation of a .java file without a public class

后端 未结 6 2015
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 06:01

Okay, so a java source file must have at least one public class and the file should be called \"class-name.java\". Fair enough.

Hence, if I have a class, th

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 06:06

    A main method is just like any other method. The only difference is that it may be invoked from the command line with the java command. Even if the main method is not visible from the command line, the class can still be used like any other Java class, and your main method may be invoked by another class in the same package. Therefore i makes sense that it compiles.

    In Java main function are not special in any sense. There just exists a terminal command that is able to invoke static methods called main...

提交回复
热议问题