Java class name and file name must be consistent), java file name

Source: Internet
Author: User

The Class Name of Java must be the same as the file name (in conversion ).

Original article: http://blog.csdn.net/shaoxiaoning/article/details/40424087

  • 1. The file name stored in 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. A Java file can only contain one public class;
  • 4. If there are more than one class in the file, the file name must be consistent with the public class name;
  • 5. If there are more than one class in the file and there is no public class, the file name can be consistent with any class name.

When writing a Java source code file, this file is usually called a compilation unit (also called a conversion unit ). Each compilation unit must have a suffix. java, but there can be a public class in the internal test of the compilation unit. The class name must be the same as the file name (including the case, but not the file suffix name. java ). Each compilation unit can have only one public class, otherwise the compiler will not accept it. If there are additional classes in the compilation unit, these classes cannot be seen in the world outside the package, because they are not public classes, they are mainly used to provide support for major public classes.

1. Each compilation unit (File) can have only one public class. This indicates that each compilation unit has a single public interface, which is represented by a public class. This interface can contain many classes that support package access permissions as required. 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 completely match the name of the file containing the compilation unit, including the case. For widgets, the file name must be Widget. java, not Widget. java or widget. java. If they do not match, a compilation error is returned.

3. Although it is not very common, it is also possible that no public class is included in the compilation unit. In this case, you can name the file at will. (Although random naming can lead to confusion when people read and maintain code .)

A Java file can have multiple classes as internal 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 place for such languages.

In reality, some very small classes are often required by several classes, so they cannot be used as internal classes, and the interface relationship must be set to public, which leads to a large number of very small classes. java program, each with only several hundred bytes.

Another problem with this type of language is that all implementation methods of a class must be included in the same file. In swing applications, it is often caused by a 100 or 200 K-byte source program. The number of lines reaches 3000 or 4000, making JBuilder extremely slow.

A. java file can have multiple classes, but only one file name can be used. 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 the class name. If there is no such name, that is, all classes have no modifiers, you can give the file a name at will, it can even be different from any class with the same name, even Chinese characters, but the suffix still needs to be available. java.

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 .)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.