Java: Special Point about Name o…

来源:互联网 发布:sql备份与恢复 编辑:程序博客网 时间:2024/06/08 13:35

Generally, there must be a public class in a compilationunit (a java file) whose name is exactly the same with the file.Like, if there is a file named Main.java, therewill be a class like:

public class Main{

    //Something

   public static void main(String...params){

       // Do something.

        System.out.println("Thisis in Main.");

   }

}

But if the specifier publicin front of Main is removed, it is also OK tocompile Main.java (with commandjavacMain.java), even to run it (withjavaMain).

What's more, when the specifier publicis removed, if we rename Main.java to Sub.java, itis still OK to compile Sub.java. However, we can not run it thistime, because  there is no classnamed Sub in Sub.java.

Thepublic can be removed, butwe are not permitted to replace it withprivate or protected in thiscase. The compilerwill complain if you try.

Iget this point from Thinkingin Java4th Edition,P230.

0 0
原创粉丝点击