Java compilation of a .java file without a public class

后端 未结 6 2016
被撕碎了的回忆
被撕碎了的回忆 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:04

    a java source file must have at least one public class and the file should be called class-name.java

    Incorrect, a top level class does not have to be declared public. The JLS states;

    If a top level class or interface type is not declared public, then it may be accessed only from within the package in which it is declared.

    See http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#104285 section 6.6.1.

提交回复
热议问题