Home > Back-end >  Java save file name must be consistent with the name of the class
Java save file name must be consistent with the name of the class

Time:09-29

Java 1. Save the file name must be consistent with the name of the class;
2. If a class file, the file name must be consistent with the name of the class;
3. A Java file can have only one public class;
4. If more than one class in the file, the file name must be consistent with the public class names;
5. If more than one class in the file, and there is no public class, the name of the file can be consistent with any class name,

When writing a Java source file, the file is usually referred to as compilation unit (sometimes referred to as the translation unit), each compilation unit must have a suffix for. Java, and in the compilation unit inside can have a public class, the class name must be the same as the file name (including the case, but not including file suffix. Java), each compilation unit can only have a public class, otherwise the compiler will not accept, if have any extra classes in that compilation unit, so in the package will not be able to see the world outside of these classes, this is 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 a public class, this means that each compilation unit has a single public interface, with a public class to performance, the interface can contain numerous support package according to the requirements of access class, if in a compilation unit has more than one public class, the compiler will give an error message,

2. The public the name of the class must be completely match the name of the file containing the compilation unit, including case, so for the Widget, the name of the file must be Widget. Java, rather than a Widget. Java or Widget. Java, if not match, will also get a compile-time error,

3. Although is not very common, but completely in the compilation unit with no public class is possible, in this case, can the file name, (despite the name makes people confused when reading and maintain code,)

A Java file can have multiple classes as an inner class/Adapter classes/other classes, but there can be only one public class, and the class name must agree and file name, this is a bad place of this kind of language,

In reality, there are often some small class need to jointly by several classes, so we can not as inner classes, and interface relations must be set to public, thus leading to a large number of small. Java programs, each only a few hundred bytes,

Another problem of this kind of language is a kind of the method to realize all must be included in the same file, in a swing application, often leads to a controlled K bytes of source program, number of lines to line, make JBuilder extremely slow,

A Java file, you can have multiple classes, but wen