設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 627|回復: 0
打印 上一主題 下一主題

Why does java file name must be same as public class name?

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2020-3-2 14:18:33 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
"Can we keep different names for java class name and java file name?"

Yes, we can keep the different name for the java filename and java class name if and only if the class is not public.

There are two cases, that we will discuss here...



          
  • Case 1: We are taking different name if the class is not public
  • Case 2: We are taking different name if the class is public

Case 1: We are taking different name if the class is not public

With the help of an example, we will see what will happen if the class is not public and in that case, we can take a different name for the java class and java file that means it is not mandatory to have the same name for the java class name and java filename in that case.

Example:
  1. class ClassIsNotPublic{
  2.         public static void main(String[] args){
  3.                 String str1 = "This class is not public so we can take different name for the java filename and java classname";
  4.                 String str2 = "This class is not prefixed with public keyword that's why it is not public";

  5.                 System.out.println("What will happen if we take non public class " +str1);
  6.                 System.out.println("Why it is not public class "+ str2);
  7.         }
  8. }
複製代碼
Output

E:\Programs>javac abc.java [abc is java filename]

E:\Programs>java ClassIsNotPublic [ClassIsNotPublic is java classname]
What will happen if we take non public class This class is not public so
we can take different name for the java filename and java classname
Why it is not public class This class is not prefixed with public keyword
that's why it is not public

Case 2: We are taking different name if the class is public

If we declared a class as "public" then, in that case, java filename and java class name must be same or in other words, we can’t take any other name for the java class name and java filename in the case of a public class.

Example:
  1. public class ClassIsPublic{
  2.         public static void main(String[] args){
  3.                 String str1 = "This class is public so we can't take different name for the java filename and java classname";
  4.                 String str2 = "This class is prefixed with public keyword that's why it is public class";

  5.                 System.out.println("What will happen if we take public class"+" " +str1);
  6.                 System.out.println("Why it is public class "+ str2);
  7.         }
  8. }
複製代碼
Output

E:\Programs>javac xyz.java
xyz.java:1: error: class IfClassIsPublic is public, should be
declared in a file named IfClassIsPublic.java
public class IfClassIsPublic{
       ^
1 error

Now we will see why it is required to take the same name for the java filename and java class name in the case of a public class?

There are few points to understand why it is required to take the same name?
  •     Let suppose we have a java file named "java1000" in that java file we have 1000 classes and in that case if we want to find any single class in 1000 classes so it will be more difficult to find and it will create a lot of confusion.
  •     In that java file we have 1000 classes we know that to find any class in 1000 classes will difficult so, in that case, almost one is class will be public and that public class will contain main() method so all the classes objects will be called from main() method class(i.e. public class) so if we take java filename and public class name will be same then it will be easy to find any class in the public class.
  •     If our java filename and public class name will be same so by using java filename we can easily reach to public class(main() method class) and if we reach to public class then from that class we can reach to any other class also from the 1000 classes and we know all classes object will be called from the main() method class(i.e. public class).




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-4-29 23:39 , Processed in 0.242480 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表