The file name saved by Java must be consistent with the class name;
2. If there is only one class in the file, the file name must be consistent with the class name;
3 .There can only be one public class in a Java file;
4. If there is more than one class in the file, the file name must be consistent with the public class name;
5. If there is more than one class in the file A class, and there is no public class, the file name can be consistent with any class name.
When writing a Java source code file, the file is usually called a compilation unit (sometimes also called a translation unit). Each compilation unit must have a suffix of .java, and there can be a public class within the compilation unit. The name of the class must be the same as the file name (including upper and lower case, but not including the suffix of the file. java) . There can only be one public class per compilation unit, otherwise the compiler will not accept it. If there are additional classes in the compilation unit, these classes are not visible to the world outside the package because they are not public classes and they are mainly used to provide support for the main public class.
\1. Each compilation unit (file) can only have one public class. This means that each compilation unit has a single public interface, represented by a public class. This interface can contain as many classes as required to support package access. If there is more than one public class in a compilation unit, the compiler will give an error message.
\2. The name of the public class must exactly match the name of the file containing the compilation unit, including case. So for Widget, the name of the file must be Widget.java, not widget.java or WIDGET.java. If they don't match, you'll also get a compile-time error.
\3. Although it is not very common, it is possible to have no public classes in the compilation unit at all. In this case, you can name the file whatever you want. (Although arbitrary naming can confuse people when reading and maintaining the code.)
A Java file can have multiple classes as inner classes/Adapter classes/other classes, but there can only be one public class. And the class name must be consistent with the file name. This is a bad thing about this type of language.
In reality, there are often some very small classes that are required by several classes at the same time, so they cannot be used as internal classes, and the interface relationship must be set to public, which results in a large number of very small classes. .Java programs, each only a few hundred bytes.
Another problem with this type of language is that all implemented methods of a class must be contained in the same file. In swing applications, it often results in a source program that is one to two hundred K bytes long, with the number of lines reaching three to four thousand, making JBuilder extremely slow.
A .java file can have multiple classes, but there can only be one file name. Which class name should be used as the file name? In fact, the file name does not matter, because when compiling with javac in the future, multiple .class files will eventually be generated, each class corresponding to a .class file. At runtime, you only need to run the corresponding .class file. At this time, the .java file is useless (not needed at runtime), so it doesn't matter what its name is.
But when your class is modified by public, the file name must be consistent with the class (for example, public class A{} can only be placed in the A.java file, otherwise when compiling with javac An error will be prompted: Class A is public and should be declared in a file named A.java). That is to say, in the same .java file, there should not be two or more public classes.
If there is a public class, the file name should be the same as the name of this class; if there is not, that is, all classes have no modifiers, then the file can be named whatever you want, or even not. It can have the same name as any class, even if it is a Chinese character name, but the suffix still needs to be .java.
Note: Many beginners (me too) may think that the file name should also be related to the class where the main method is located, but this is not the case. The main method is just an entrance to the program. The teacher said "a program can only have one entrance", which seems to mean 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. Provide N multiple entrances to the program, but in the end when you run the program, you can only enter from one of them. This is the role of main (program entrance). (This is why you will find that when programmers do unit testing, they will add main methods to many classes they make, because they want to add running entrances to the things they make, so as to facilitate testing.)