title: How to avoid a missing return statement in an if
body: <p>I want to return a Course object with this method but only if the object isn't null.</p>
  
  <pre><code>public Course getClass1()
  {
      if(class1 != null)
      {
          return class1;
      }
  }
  </code></pre>
  
  <p>I have a 'missing return statement' error because it may not return anything, but I don't want to return anything, not even an empty String, if the object is null. I am okay with  changing the method to a void</p>
  
  <pre><code>public void getClass1()
  
  {
      if(class1 != null)
      {
          System.out.println(class1);
      }
  }
  </code></pre>
  
  <p>But if I do that I can't call the methods in my toString</p>
  
  <pre><code>public String toString()
  {
      return super.getName() + "\t" + super.getAge() + "\t" + getStudentNumber() +
              "\n" + getClass1() + "\n" + getClass2() + "\n" + getClass3();
  }
  </code></pre>
  
  <p>because void types aren't allowed.</p>