JAVA write a file more than one class

JAVA write a file more than one class, and the class is at the same level, should pay attention to:

  • .Java files in a peer can have a plurality of classes, which can only modifiers public / abstract / final / no modifiers and
  • Modified public can have only one, and must be the same as the file name;
  • Because the virtual machine jvm looks for classes in order to improve the speed, when using the import statement to import, import only the file name corresponding to the space corresponding to the class file, but we all have to use a public document, and therefore directly into the class corresponding to the class name files.
  • If the public can not have a different file name

    Java compiler at compile time, if the entire Java files (compilation unit) are not public class (external public interface class), the class loader son do not need this regard directly to load all of the compiled byte code generation unit file (.class files), it is no need to look after compiling bytecode file location. The class name and the file name is the same for convenience of the virtual machine to find the corresponding class bytecode files corresponding to the respective paths. So there is no public Java class file, the file name and class name are no contact.

     

 

  • Can call each other between the peer class file, but in addition to the public, Other other files can not be called
    in a .java files by the class / Enum / interfaces / Anontation wherein the at least one type of composition. The method of single / variable can not exist alone in the file, so that the package is made of common methods class. The reason is that java is class loading mechanism, you need to compile a java class file into multiple files, when the class to use.
    When compiled with the javac .java file, it will give every class produces a .class file

 

Original link: https: //blog.csdn.net/q5706503/article/details/84349692

Guess you like

Origin www.cnblogs.com/jerrys/p/11941751.html