title: Static class instance inside class
body: <p>I am having a difficult time understanding the way static classes are using in the following scenario.</p>
  
  <p>Let’s say I declare a class as follows:</p>
  
  <pre><code>public class TestLibrary {
  
      private static final TestLibrary library = new TestLibrary();
      private ErrorHandler errorHandler = null;
  
      public static TestLibrary getLibrary() {
          return library;
      }
      public ErrorHandler getErrorHandler() {
          return errorHandler;
      }
  
      public int run(String[] args) {
          this.initialize(args);
      }
  
      private void initialize(String[] data) {
          library.errorHandler = new ErrorHandler();
      }   
  }
  </code></pre>
  
  <p>I now change the class slightly to</p>
  
  <pre><code>private void initialize(String[] data) {
      errorHandler = new ErrorHandler();
  }
  </code></pre>
  
  <p>I declare errorHandler in other classes as follows:</p>
  
  <pre><code>private ErrorHandler errorHandler = TestLibrary.getLibrary().getErrorHandler();
  </code></pre>
  
  <p>My code ultimately still functions the same when I use the instance of <code>errorHandler</code> in other classes, but I don’t understand why.</p>
  
  <p>Question 1: Shouldn’t the 2nd case create a <code>new ErrorHandler</code> that is part of the object <code>TestLibrary.errorHandler</code> rather than <code>library.errorHandler</code>?</p>
  
  <p>Question 2: Am I doing something wrong?  Could you elaborate?</p>