title: Understanding Java Packages and Factories
body: <p>I'm reading Bruce Eckel's <a href="http://www.mindviewinc.com/Books/TIJ4/" rel="nofollow">Thinking in Java</a> and there's an exercise I'm just not getting:</p>
  
  <blockquote>
    <p>Pg. 161:  Exercise 8: (4) Following
    the form of the example Lunch.java,
    create a class called
    ConnectionManager that manages a fixed
    array of Connection objects. <strong>The
    client programmer must not be able to
    explicitly create Connection objects</strong>,
    but can only get them via a static
    method in ConnectionManager. When the
    ConnectionManager runs out of objects,
    it returns a null reference. Test the
    classes in main( ).</p>
  </blockquote>
  
  <p>I came up with the following solution:</p>
  
  <pre><code>// TestConnection.java
  import java.util.*;
  
  public class TestConnections {
      public static void main( String[] args ) {
          Connection cn = Connection.makeConnection();
  
          for (int i = 0; i != 6; ++i) {
              Connection tmp = ConnectionManager.newConnectiton();
              if ( tmp == null )
                  System.out.println("Out of Connection objects");
              else {
                  System.out.println("Got object: " + tmp );
              }
          }
      }
  }
  </code></pre>
  
  <p>And a second file in the same directory meaning everything ends up in the same default package:</p>
  
  <pre><code>// ConnectionManager.java
  class Connection { 
      private Connection() {}
  
      static Connection makeConnection() {
          return new Connection();
      }
  }
  
  public class ConnectionManager {
      static private Connection[] _connections = new Connection[5];
  
      private ConnectionManager() {}
  
      static public Connection newConnectiton() {
          for ( int i = 0; i != _connections.length; ++i ) {
              if ( _connections[i] == null ) {
                  _connections[i] = Connection.makeConnection();
                  return _connections[i];
              }
          }
          return null;
      }
  }
  </code></pre>
  
  <p>The thing is that the client program can directly create Connection objects via the static <code>Connection.makeConnection</code> factory, which seems to violate the exercises goals.  Yet if I make the ConnectionManager.java a separate package then import it complains that it can't find a definition for <code>Connection</code>.</p>
  
  <p><strong>I feel like something is going over my head here, but I'm not sure what.</strong></p>
  
  <p>Here is the code for Lunch.java which is referenced in the question:</p>
  
  <pre><code>//: access/Lunch.java
  // Demonstrates class access specifiers. Make a class
  // effectively private with private constructors:
  
  class Soup1 {
    private Soup1() {}
    // (1) Allow creation via static method:
    public static Soup1 makeSoup() {
      return new Soup1();
    }
  }
  
  class Soup2 {
    private Soup2() {}
    // (2) Create a static object and return a reference
    // upon request.(The "Singleton" pattern):
    private static Soup2 ps1 = new Soup2();
    public static Soup2 access() {
      return ps1;
    }
    public void f() {}
  }
  
  // Only one public class allowed per file:
  public class Lunch {
    void testPrivate() {
      // Can't do this! Private constructor:
      //! Soup1 soup = new Soup1();
    }
    void testStatic() {
      Soup1 soup = Soup1.makeSoup();
    }
    void testSingleton() {
      Soup2.access().f();
    }
  } ///:~
  </code></pre>