title: Is it a good practice to use Optional as an attribute in a class?
body: <p>I have read something about the purpose of <code>Optional</code> (unfortunately I don't remember where) in Java 8, and I was surprised the writer didn't mention the use of an <code>Optional</code> as an attribute in a class.</p>
  
  <p>Since I am using optionals pretty frequently in my classes, I was wondering if this is a good practice. Or could I better just use normal attributes, which return <code>null</code> when they are not set?</p>
  
  <p><strong>Note:</strong> It may look like my question is opinion based, but I get the feeling using <code>Optional</code> in a class is really not the way to go (after reading the mentioned post). However, I like to use it and can't find any downside of using it.</p>
  
  <p><strong>Example</strong></p>
  
  <p>I would like to give an example to clarify. I have a class <code>Transaction</code>, which is built like this:</p>
  
  <pre><code>public class Transaction {
  
       private Optional&lt;Customer&gt; = Optional.empty();
       ....
  </code></pre>
  
  <p><strong>vs</strong></p>
  
  <pre><code>public class Transaction {
  
       private Customer = null;
       ....
  </code></pre>
  
  <p>When checking on a <code>Customer</code>, I think it is most logical to use <code>transaction.getCustomer().isPresent()</code> than <code>transaction.getCustomer() != null</code>. In my opinion the first code is cleaner than the second one.</p>