One. java files can have multiple classes (a maximum of one public modifier can be added, while other classes cannot be added with a private modifier), but only one file name can be added, which class name is used as the file name?
In fact, the file name does not matter, because in the future, when javac is used for compilation, multiple. class files will be generated, and each class corresponds to a. Class file. During the runtime, you only need to run the corresponding. Class file. At this time, the. Java file is useless (not required during the runtime). What is its name.
However, when your class is public, the file name must be consistent with the class name (for example, public class A {} can only be placed in. in the Java file, otherwise the error will be prompted during javac Compilation: Class A is public and should be named. java file ).
In other words, two or more public classes should not appear in the same. Java file. If there is a public class, the file name should be the same as this class name; if there is no such class, that is, all classes have no modifier, then you can give the file casually
The name can even be different from the same name of any class, even the name of a Chinese character, but the suffix still needs to have. Java (haha, try it if you don't believe it ).
Note: Many beginners (I am also) may think that the file name should also be related to the class where the main method is located. The main method is just an entry to the program. The teacher says, "One program can only have one entry." It seems that there can only be one main method. In fact, as long as you are happy, you can write the main method in all classes to provide N multiple entries for the program. However, when you run the program, you can only enter one of them, this is the main function (program entry ). (That's why you find that when a programmer performs a unit test, he adds the main method to many of his classes, because he wants to add a running entry for his own work, to facilitate testing .)
For a public class, it can be referenced by any class in the project. You only need to import the class file corresponding to it before using it. You can easily find the class information in the corresponding path (package name) by matching the class name with the file name. If you do not do this, it will be difficult to find it, and the overhead will be high.
Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Question 1: If no access modifier is added before the class, what is the default access permission (can it be accessed by its subclass in other packages ?) Question 2: If no access modifier is added before a class member variable, what is the default access permission? Question 3: If no access modifier is added before the class member method, what is the default access permission?
Class and Class Members do not have modifier: friendly:
Only this class and the class in the same package can be accessed. Others have no access permissions.
If a member of a class does not have any permission modification, the default package access permission is set to friendly. Note that friendly is not a keyword in Java, here is my favorite way to use it. Other classes in the same package can be accessed, but not outside the package. For classes in the same folder that do not use a package, Java will automatically call friendly Members in the class as the defaultpackage that is affiliated to the directory. For example, the two classes are in two files in the same folder, although the package is not introduced, but belong to the same defaultpackage.