title: Scope of object within an object in Java
body: <p>I'm learning Java at the moment so I hope this question isn't too obvious. I come from another language which does not have garbage collection.
  In this other language I sometimes created objects in constructor and then deleted them in the destructor so I could use them for the entire life of the object.</p>
  
  <p>As a simplified example, I have a user and a booking class. The booking class references a user but if I create the user in the constructor of the booking class, it dereferences the user once it leaves the constructor and becomes out of scope. Any future reference call to the booking.bookedBy user then returns null.</p>
  
  <pre><code>class user {
      public String username;
      public String displayName;
      user(Connection conn, String usernameIn){
       username = usernameIn;
           ... do DB stuff to populate attributes
      }
  }
  
  class booking {
    int bookingID;
    user bookedBy;
    ...
    booking(Connection conn, int bookedIDIn){
       bookingID = bookedIDIn;
        ...do DB stuff to populate attributes and grab bookedByUserID
        ...field value and build the BookedByUsername
       user bookedBy = new user (bookedByUsername)
    }
  }
  </code></pre>
  
  <p>Is there a way around this? Or do I need to rethink my design?</p>