Item 25: Limit source files to a single top-level class

Never put multiple top-level classes or interfaces in a single source file. Following this rule guarantees that you can’t have multiple definitions for a single class at compile time.

// 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"; 
  } 
}

Leave a Reply

Your email address will not be published. Required fields are marked *