Peter
Peter

Reputation: 3

Scope of object within an object in Java

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?

Upvotes: 0

Views: 3685

Answers (4)

Synesso
Synesso

Reputation: 39018

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

Upvotes: 1

Chris J
Chris J

Reputation: 9262

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

Upvotes: 1

wkl
wkl

Reputation: 80041

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

Upvotes: 3

Joseph
Joseph

Reputation: 25533

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

Upvotes: 2

Related Questions