在对象生命周期的哪个点使用匿名内部类作为PropertyChangeListener时,是否收集了类垃圾?包含类(SettingsNode)被回收后?我应该显式移除包含类(SettingsNode)的终结器中的PropertyChangeListener吗?Java - 匿名内部类生命周期
public class SettingsNode extends AbstractNode
{
public SettingsNode(Project project, ProjectSettings projectSettings)
throws IntrospectionException
{
// use an anonymous inner class to listen for changes
projectSettings.addPropertyChangeListener(ProjectSettings.PROP_NAME,
new PropertyChangeListener()
{
@Override
public void propertyChange(PropertyChangeEvent evt)
{
// handle event
}
});
}
}
根据提供的答案,匿名似乎不是此用例的最佳选项。我现在认为一个实例内部类可能是一个更好的选择,因为我可以保存一个引用,并在将来的某个时刻调用removePropertyListener方法。 – javacavaj 2010-01-20 20:39:25