The relationship between the class name and the file name in Java

Source: Internet
Author: User

1.Java the file name must be the same as the class name;
2. If there is only one class in the file, the file name must match the class name;
3. There can be only one public class in a Java file;
4. If there is more than one class in the file, the filename must match the public class name;
5. If there is more than one class in the file, and there is no public class, the file name can match any class name.

When writing a Java source code file, this file is often referred to as a compilation unit (sometimes referred to as a translation unit). Each compilation unit must have a suffix of. java, and within the compilation unit there can be a public class that has the same name as the file name (including case, but not the file's suffix. java). Each compilation unit can have only one public class, or the compiler will not accept it. If there are additional classes in the compilation unit, these classes are not visible in the world outside the package because they are not public and they are primarily used to support the main public classes.

1. Each compilation unit (file) can have only one public class. This means that each compilation unit has a single common interface, which is represented by the public class. The interface can contain numerous classes that support package access, as required. If there is more than one public class within a compilation unit, the compiler gives the error message.

2. The name of the public class must match exactly the file name that contains the compilation unit, including case. So for widgets, the name of the file must be Widget.java, not Widget.java or Widget.java. If they do not match, they will also get a compile-time error.

3. Although not very common, it is possible that the compilation unit is completely without the public class. In this case, you can name the file arbitrarily. (although arbitrary naming can cause confusion when reading and maintaining code.) )

A Java file can have more than one class as an inner class/adapter class/Other class, but there can be only one public class, and the class name must match the file name. This is a bad place for this kind of language.

In reality, there are often very small classes that are required by several classes, and therefore cannot be used as internal classes, and interface relationships must be set to public, resulting in a large number of small ones. Java programs, each with only hundreds of bytes.

Another problem with this type of language is that all implementations of a class must be contained in the same file. In swing applications, it often results in a source program of up to one hundred or two hundred K bytes, with rows of up to three thousand or four thousand rows, making jbuilder extremely slow.

A. java file can have multiple classes, but only one file name, and which class name to use as the file name? In fact, the file name does not matter, because in the future compile with Javac, will eventually generate multiple. class files, each of which corresponds to a. class file. The runtime can run as long as the corresponding. class file is running, and the. java file is useless (not required at runtime), and it's called by name.

However, 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, or you will be prompted with an error when compiling with Javac: Class A is common and should be declared in a file named A.java. That is, in the same. java file, there should not be 2 or more than 2 public classes.

If there is a public class class, you should let the file name and the same name, if not, that is, all classes do not have modifiers, then you can give the file name, or even do not have the same name as any of the class name, even if the Chinese names can be, but the suffix should have. java.

Note: There are a lot of beginners (I also) may think that the file name should also be related to the class of the main method, not actually. The main method is just an entrance to the program, the teacher said "a program can only have one entrance", as if to say there can only be a main method, in fact, as long as you are happy, you can write in all classes of the Main method, to provide the program n multiple portals, But in the end when you run the program you can only go in from one of these entrances, which is the function of main (program entry). (That's why you'll find that when a programmer does a unit test, he adds the main method to many of his own classes, because he has to add a running portal for what he's doing, so it's easy to test.) )

Transferred from: http://blog.163.com/frances_0123/blog/static/1841316562012229103014622/

The relationship between the class name and the file name in Java

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.