title: How to avoid tedious null checks on an object
body: <p>If I have a class that I want to accept an optional logger to log debug information:</p>
  
  <pre><code>public class A {
  
  private Logger logger ; 
  
  public A( Logger logger ) { 
      this.logger = logger ; 
  }
  
  public A() { 
      this( null ) ;
  }
  
  public void hello() {
      logger.debug( "hello" ) ;
  }
  
  public void goodbye() {
      logger.warn( "goodbye" ) ; 
  }
  
  }
  </code></pre>
  
  <p>Is there any way for me to avoid the need for constant null checks?</p>
  
  <pre><code>public void hello() {
  
      if( logger != null ) { 
          logger.debug( "hello" ) ;
      }
  
  }
  
  public void goodbye() {
  
      if( logger != null ) {
          logger.warn( "goodbye" ) ; 
      }
  
  }
  </code></pre>
  
  <p>I thought maybe if I made a logger wrapper class, that could do the checks for me and then either do nothing, or log the message. But then I would need to wrap each method I would want to use.</p>