分配新对象是否会自动销毁先前分配给同一数据成员的对象?
请考虑以下两个案例,帮我一劳永逸地消除我心中的疑问。
情况 1:
public class Activity {
WebView mWebview;
@Override
public void onCreate(Bundle savedInstanceState) {
initWebView();
}
public void initWebView() {
mWebView = (WebView) findViewById(R.id.webview);
}
}
findViewById()
是否实例化一个 第二次新的WebView
对象 它叫什么?- 如果答案是“是”,就是旧的
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);
}
}
- 当
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);
}
}
- Does
findViewById()
instantiate a
newWebView
object the second time
it is called? - If the answer is 'yes', is the old
WebView
object (that was previously
assigned tomWebView
) 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);
}
}
- When
initWebView()
is called a
second time, is the oldMyPictureListener
object (that was
previously assigned tomPictureListener
) automatically
destroyed? (i.e. gets lost by being
placed in a queue for garbage
collection)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
情况 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 setmWebView
tonull
, the Object still exists with references to it from the Activity's UI, you just can't access it frommWebView
anymore.Case 2: Yes (insofar as 'automatically destroyed' works in Java). Your
mWebView
has a handle (a pointer) to aMyPictureListener
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.