Item 25: The source file is limited to a single top-level classes

ITEM 25: LIMIT SOURCE FILES TO A SINGLE TOP-LEVEL CLASS
  Although the Java compiler allows you to define multiple top-level class in a source file, but this is not any good, but there is a big risk. Risk stems from the fact that: define multiple top-level classes in the source file so as to provide more of a class defined as possible. Which is defined by the order of the source files passed to the impact of the compiler.
  To make it concrete, consider the source file, it contains only a reference to the main class of the other two top members of the class:

public class Main {
  public static void main(String[] args) {
    System.out.println(Utensil.NAME + Dessert.NAME); 
  }
}

  Now suppose you have defined and Utensil Dessert in the source file named Utensil.java in.

// Two classes defined in one file. Don't ever do this!
class Utensil {
  static final String NAME = "pan"; 
}
class Dessert {
  static final String NAME = "cake";
}

Of course, the main program will output "pancake".
Now, suppose you accidentally create another source file named Dessert.java, which defines the same two classes:

// Two classes defined in one file. Don't ever do this!
class Utensil {
  static final String NAME = "pot";
}
class Dessert {
  static final String NAME = "pie";
}

  If you are lucky enough, you can use the command javac Main.java Dessert.java compile the program will fail to compile, the compiler will tell you that you have defined multiple Utensil and Dessert. This is because the compiler will first compile Main.java, when it sees for Utensil references (references to prior to the Dessert), it will find the class in Utensil .java in and find Utensil and Dessert. When the compiler encounters Dessert.java on the command line, it will also pull into the file, so that it simultaneously met the definition of Utensil and Dessert.
  If you use javac Main.java or javac Main.java Utensil.java command compiler, its behavior before writing Dessert.java files, print "pancake". However, if you use the javac Dessert.java Main.java command compiles the program, it will print "potpie". Thus, the behavior is influenced by the order of transfer source file to the compiler, which is clearly unacceptable.
  Fix this problem is very simple, just the top-level classes (in our example is Utensil and Dessert) split into separate source files. If you want multiple top-class into a single source file, consider using a static class members (item 24) as the class split into separate source files alternatives. If the class is a slave of another class, then convert them to a static member of a class is usually a better choice because it enhances readability, and can be reduced to a private accessibility (item 15 classes by declaring class ). Here is an example of static class members:

// Static member classes instead of multiple top-level classes
public class Test {
  public static void main(String[] args) { 
    System.out.println(Utensil.NAME + Dessert.NAME);
  }
  private static class Utensil {
    static final String NAME = "pan";
  }
  private static class Dessert {
    static final String NAME = "cake";
  } 
}

  The lesson is clear: Never use multiple top-level class or interface in a source file. Follow this rule ensures that you can not define multiple definitions for a single class at compile time. This in turn guarantee the behavior and generate class files compiled program is independent of the source file is passed to the order of the compiler.

Guess you like

Origin blog.csdn.net/weixin_33787529/article/details/90973304
Recommended