简体   繁体   English

Java对象中对象的范围

[英]Scope of object within an object in Java

I'm learning Java at the moment so I hope this question isn't too obvious. 我目前正在学习Java,所以我希望这个问题不太明显。 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. 然后,将来对booking.bookedBy用户的任何参考调用都将返回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? 还是我需要重新考虑我的设计?

You are creating a new bookedBy user variable in your constructor, rather than using your class' member variable. 您正在构造函数中创建一个新的bookedBy用户变量,而不是使用类的成员变量。

You probably want to change: 您可能要更改:

user bookedBy = new user(bookedByUsername);

with: 与:

bookedBy = new user(bookedByUsername);

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

In your booking class, you actually have declared two variables called user bookedBy. 在预订类中,您实际上已经声明了两个名为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)
  }
}
 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)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM