views:

65

answers:

4

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.

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.

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)
  }
}

Is there a way around this? Or do I need to rethink my design?

+2  A: 

You're declaring a local variable in your constructor and it's being used to assign the user you create in the constructor.

I think you want this:

class booking {
  int bookingID;
  user bookedBy;
  ...
  booking(Connection conn, int bookedIDIn){
     bookingID = bookedIDIn;
     //there's no declaration of type needed here because 
     //you did that earlier when you declared your member variable up top.
     bookedBy = new user (bookedByUsername) 
  }
}
Joseph
Or, if your tastes run that way, the Python-infuenced `this.bookedBy=...`, which avoids the ambiguity.
bobince
Good idea. I might use "this" especially when I'm getting use to the language.
Peter
+3  A: 

You are creating a new bookedBy user variable in your constructor, rather than using your class' member variable.

You probably want to change:

user bookedBy = new user(bookedByUsername);

with:

bookedBy = new user(bookedByUsername);

birryree
Excellent that works. Shows my inexperience using Java. I guess I'm use to Delphi where you declare all local variables at the top of the method.Thanks for the pointer.
Peter
+1  A: 

In your booking class, you actually have declared two variables called user bookedBy. One has scope for the entire booking class and one has scope for the constructor. To fix this problem, you need to remove the variable declaration in your constructor as show below:

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
    bookedBy = new user (bookedByUsername)
  }
}
Chris J
+1  A: 
 user bookedBy;

and

user bookedBy = new user (bookedByUsername)

are two different variables.

Remove the second type declaration and your user instance will be allocated to the field level. ie:

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
     bookedBy = new user (bookedByUsername)
  }
}
Synesso