Read thinking in Java This book, there is such a phrase "inside a compilation unit, you can have a public class, it must have the same name as the file"
Have the following questions:
- In a class that says you can have a public class, is that what it means?
The following experiment was carried out with this problem: (filename is Test1.java)
classTest1 { Public Static voidMain (string[] args) {if(Args! =NULL&& args.length > 0) {System.out.println (args.length); for(intI=0;i < args.length;i++) {System.out.println ("args[" +i+ "]:" +Args[i]); } } } Public Static voidTest () {}}classtest3{ Public Static voidMain (string[] args) {if(Args! =NULL&& args.length > 0) {System.out.println (args.length); for(intI=0;i < args.length;i++) {System.out.println ("args[" +i+ "]:" +Args[i]); } } } Public Static voidTest () {}}
Execute the command under the terminal without any errors, it can be seen that the first one is really feasible, and there is really no need for a public to specify the relevant class name in a class file.
Then the second question is, since there is no public modification, it is not inside the class name can also be arbitrary, and then modify the above code in TEST1 to Test2 and test, the result is as follows:
The same results are almost the same, so it can be concluded that there is no public-decorated class in a class file, and the class name does not have to be the same as the file name.
Through the above research can be clearly seen in the "Thinking in Java" in a mistake, the exact words are as follows:
For example, assume that the file name is Myclass.java. It means that there is one and only one public class in that file. And the name of that class must be MyClass (including case) "
--taken from 5.1 knots
Extended Understanding:
The following is a list of the class name modifiers (taken from Baidu)
public Common class default class (default modifier, only objects in the same package can use such a class) abstract class final cannot be inherited class static static class private can only be decorated with internal classes, generally not recommended
The idea of Java programming that you don't know