java类名与文件名_为什么Java文件名必须与公共类名相同?
java類名與文件名
The question is that "Can we keep different names for java class name and java file name?"
問題是“我們可以為Java類名和Java文件名保留不同的名稱嗎?”
Yes, we can keep the different name for the java filename and java class name if and only if the class is not public.
是的,當(dāng)且僅當(dāng)該類不是公共類時(shí),才能為java文件名和Java類名保留不同的名稱。
There are two cases, that we will discuss here...
有兩種情況,我們將在這里討論...
Case 1: We are taking different name if the class is not public
情況1:如果該課程不是公開的,我們將使用其他名稱
Case 2: We are taking different name if the class is public
情況2:如果該課程是公開的,我們將使用其他名稱
Case 1: We are taking different name if the class is not public
情況1:如果該課程不是公開的,我們將使用其他名稱
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.
借助示例,我們將看到如果該類不是公共類,會(huì)發(fā)生什么情況,在這種情況下,我們可以為java類和java文件取一個(gè)不同的名稱,這意味著不必強(qiáng)制為該類使用相同的名稱。在這種情況下,java類名稱和java文件名。
Example:
例:
class ClassIsNotPublic{public static void main(String[] args){String str1 = "This class is not public so we can take different name for the java filename and java classname";String str2 = "This class is not prefixed with public keyword that's why it is not public";System.out.println("What will happen if we take non public class " +str1);System.out.println("Why it is not public class "+ str2);} }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 publicCase 2: We are taking different name if the class is public
情況2:如果該課程是公開的,我們將使用其他名稱
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.
如果我們將某個(gè)類聲明為“ public”,則在這種情況下,java文件名和Java類名必須相同或換句話說,對于Java類名和java文件名,我們不能使用任何其他名稱。公共課。
Example:
例:
public class ClassIsPublic{public static void main(String[] args){String str1 = "This class is public so we can't take different name for the java filename and java classname";String str2 = "This class is prefixed with public keyword that's why it is public class";System.out.println("What will happen if we take public class"+" " +str1);System.out.println("Why it is public class "+ str2);} }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 errorNow 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?
現(xiàn)在,我們將看到為什么在公共類的情況下,要求Java文件名和Java類名使用相同的名稱?
There are few points to understand why it is required to take the same name?
有幾點(diǎn)要理解為什么需要使用相同的名稱?
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.
假設(shè)我們有一個(gè)名為“ java1000”的java文件,該java文件中有1000個(gè)類,在這種情況下,如果我們想在1000個(gè)類中找到任何一個(gè)類,那么將很難找到它,并且會(huì)造成很多混亂。
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.
在該java文件中,我們有1000個(gè)類,我們知道很難在1000個(gè)類中找到任何一個(gè)類,因此,在那種情況下,幾乎是class是public,而public class將包含main()方法,因此所有class對象都是從main()方法類(即公共類)調(diào)用,因此,如果我們使用java文件名,并且公共類名稱相同,則可以很容易在公共類中找到任何類。
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).
如果我們的java文件名和公共類名相同,那么通過使用java文件名,我們可以輕松地訪問public class(main()方法類);如果我們訪問public類,那么從該類中我們也可以訪問任何其他類。 1000個(gè)類,我們知道所有類對象都將從main()方法類(即公共類)中調(diào)用。
翻譯自: https://www.includehelp.com/java/why-does-java-file-name-must-be-same-as-public-class-name.aspx
java類名與文件名
總結(jié)
以上是生活随笔為你收集整理的java类名与文件名_为什么Java文件名必须与公共类名相同?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最佳的一击剧情介绍
- 下一篇: 80版本魔王契约怎么办?等级一旦过线野猪