Home > Java > javaTutorial > body text

Why should the java file name be the same as the class name?

(*-*)浩
Release: 2019-05-22 20:45:03
Original
6668 people have browsed it

Because: Java is interpreted and executed. It does not put all class files into memory during runtime. Instead, it goes to the corresponding file directory to find the corresponding class file when an import is encountered.

Why should the java file name be the same as the class name?

For a public class, it can be referenced by any class in the project. Just import its corresponding class file before using it. One-to-one correspondence between class names and file names can facilitate the virtual machine to find the corresponding class information in the corresponding path (package name). If you don't, it will be difficult and expensive to find.

Summary:

In a source file (.java), classes that are not modified with public can also have the same file name.

There can be inconsistencies between the class name and the file name in a source file, but it cannot be modified with public.

If there is no public modified class in a package, then I think this package I It seems meaningless, because the interface cannot be provided for "customer programmers". Even if the class that is not modified with public contains a public-modified static method, "customer programmers" do not have permission to access it.

Classes whose source files are not modified with public are mainly "support" for classes modified with public.

It is best not to write many classes in a .java source file.

The main method does not necessarily have to be executed under a public modified class. But the main method must be public modified

"Thinking in Java" explanation:

为Java创建一个源码文件的时候,它通常叫作一个“编辑单元”(有时也叫作“翻译单元”)。每个编译单元都必须有一个以.java结尾
的名字。而且在编译单元的内部,可以有一个公共(public)类,它必须拥有与文件相同的名字(包括大小写形式,但排除.java文件扩
展名)。如果不这样做,编译器就会报告出错。每个编译单元内都只能有一个 public类(同样地,否则编译器会报告出错)。那个编译
单元剩下的类(如果有的话)可在那个包外面的世界面前隐藏起来,因为它们并非“公共”的(非public),而且它们由用于主public
类的“支撑”类组成。编译一个.java文件时,我们会获得一个名字完全相同的输出文件;但对于.java文件中的每个类,它们都有一个.
class扩展名。因此,我们最终从少量的.java文件里有可能获得数量众多的.class文件。如以前用一种汇编语言写过程序,那么可能已
习惯编译器先分割出一种过渡形式(通常是一个.obj文件),再用一个链接器将其与其他东西封装到一起(生成一个可执行文件),或
者与一个库封装到一起(生成一个库)。但那并不是Java的工作方式。一个有效的程序就是一系列.class文件,它们可以封装和压缩到
一个JAR文件里(使用Java1.1提供的jar工具)。Java解释器负责对这些文件的寻找、装载和解释(注释①)。 
①:Java并没有强制一定要使用解释器。一些固有代码的Java编译器可生成单独的可执行文件。
Copy after login

The above is the detailed content of Why should the java file name be the same as the class name?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template