分配新对象是否会自动销毁先前分配给同一数据成员的对象?

发布于 2024-11-04 11:05:27 字数 1079 浏览 0 评论 0原文

请考虑以下两个案例,帮我一劳永逸地消除我心中的疑问。

情况 1:

public class Activity {
  WebView mWebview;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    initWebView();
  }

  public void initWebView() {
    mWebView = (WebView) findViewById(R.id.webview);
  }

}
  1. findViewById() 是否实例化一个 第二次新的 WebView 对象 它叫什么?
  2. 如果答案是“是”,就是旧的 WebView 对象(之前是 自动分配给mWebView) 被毁了? (即因被迷失 放入垃圾队列 时

情况 2:

public class Activity {
  WebView mWebview;
  MyPictureListener mPictureListener;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    initWebView();
  }

  public void initWebView() {
    mWebView = (WebView) findViewById(R.id.webview);
    mPictureListener = new MyPictureListener(mWebView);
    mWebView.setPictureListener(mPictureListener);
  }

}
  1. initWebView() 被调用 第二次了,还是旧的 MyPictureListener 对象(即 之前分配给 mPictureListener)自动 被毁了? (即因被迷失 放入垃圾队列 收藏)

Please help me clear this doubt I have in my head once and for all, considering the following two cases.

Case 1:

public class Activity {
  WebView mWebview;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    initWebView();
  }

  public void initWebView() {
    mWebView = (WebView) findViewById(R.id.webview);
  }

}
  1. Does findViewById() instantiate a
    new WebView object the second time
    it is called?
  2. If the answer is 'yes', is the old
    WebView object (that was previously
    assigned to mWebView) automatically
    destroyed? (i.e. gets lost by being
    placed in a queue for garbage
    collection)

Case 2:

public class Activity {
  WebView mWebview;
  MyPictureListener mPictureListener;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    initWebView();
  }

  public void initWebView() {
    mWebView = (WebView) findViewById(R.id.webview);
    mPictureListener = new MyPictureListener(mWebView);
    mWebView.setPictureListener(mPictureListener);
  }

}
  1. When initWebView() is called a
    second time, is the old
    MyPictureListener object (that was
    previously assigned to
    mPictureListener) automatically
    destroyed? (i.e. gets lost by being
    placed in a queue for garbage
    collection)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2024-11-11 11:05:27

情况 1:无论调用 findViewById(R.id.webview) 多少次,返回的对象都是 UI 框架膨胀布局时创建的同一个 WebView。当您将它分配给变量时,您只是说“我想要该对象的句柄”,仅此而已。如果将 mWebView 设置为 null,该对象仍然存在,并从 Activity 的 UI 引用它,只是无法从 mWebView 访问它不再了。

情况 2:是的(只要“自动销毁”在 Java 中有效)。您的 mWebView 有一个指向 MyPictureListener 对象的句柄(指针),当您将其分配给该类的新实例时,对旧对象的引用将丢失,并且旧对象现在是垃圾收集的候选者。

这实际上是对指针如何工作的[粗略概括]的迷你解释,这是我听到有些人说 Java 不使用的概念。这是完全错误的,熟悉 C++ 中使用的指针将使您将来更好地了解此类问题。

Case 1: No matter how many times you call findViewById(R.id.webview), the object returned is the same WebView that was created when the layout was inflated by the UI framework. When you assign it to your variable, you're just saying "I want a handle to that Object," and that's all it is. If you set mWebView to null, the Object still exists with references to it from the Activity's UI, you just can't access it from mWebView anymore.

Case 2: Yes (insofar as 'automatically destroyed' works in Java). Your mWebView has a handle (a pointer) to a MyPictureListener Object, when you assign it to a new instance of that class, the reference to the old Object is lost, and the old Object is now a candidate for garbage collection.

This is really a [grossly generalized] mini-explanation of how pointers work, which is a concept I've heard some people say that Java doesn't use. That's completely erroneous, and getting comfortable with pointers as they are used in C++ will give you much better insight into these kinds of questions in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文