本文介绍了Java匿名对象和垃圾收集部分-2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人学生student = new Student(); 

public Student getStudent(){
return student;


$ b public void function(){
getStudent()。setName(john);
}

public void function(){
Student student_local = getStudent();
student_local.setName(john);
}




解决方案

Amit我看过你的其他问题,重要的是永远只有可达性,不管你如何使用student,它总是可以到达的,直到包含它的类可到达为止。
$ b

更新,关于您添加的问题:

既不是因为你在这两个函数中做了同样的事情, t真的知道你的对象什么时候会被收集,而这对于这类例子来说并不重要。



阅读关于G C作品:

编辑:应该学会阅读这些问题,阅读这个修改我的答案,对不起Amit 。


private Student student = new Student();

public Student getStudent(){
    return student;
}


public void function(){
   getStudent().setName("john");
}

public void function(){
   Student student_local = getStudent();
   student_local.setName("john");
}
解决方案

Amit i've seen your other question, what matters is always only reachability, it doesn't matter how you use student, it will always be reachable until the class that contains it is reachable.

Update, regarding the question you added:

Neither because you do the same thing in both functions and remember that you can't really know when your objects will be collected, and it doesn't even matter for this kind of examples.

Read this description of how GC works: http://www.cubrid.org/blog/dev-platform/understanding-java-garbage-collection/

Edited: Should learn to read the questions, read this revision of my answer, sorry Amit.

这篇关于Java匿名对象和垃圾收集部分-2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 06:48