Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Anonymous Inner Class Life Cycle

Tags:

java

When using an anonymous inner class as a PropertyChangeListener at what point in the object life cycle is the class garbage collected? After the containing class (SettingsNode) is reclaimed? Should I explicitly remove the PropertyChangeListener in the finalizer of the containing class (SettingsNode)?

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
                }
            });
     }
}
like image 733
javacavaj Avatar asked Jan 20 '10 20:01

javacavaj


People also ask

What is anonymous inner class in Java?

It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class.

Is an anonymous class an inner class?

Anonymous classes are inner classes with no name. Since they have no name, we can't use them in order to create instances of anonymous classes. As a result, we have to declare and instantiate anonymous classes in a single expression at the point of use. We may either extend an existing class or implement an interface.

Which is true about an anonymous inner class Java?

Ans: C Explanation: Option C is correct because the syntax of an anonymous inner class allows for only one named type after the new, and that type must be either a single interface (in which case the anonymous class implements that one interface) or a single class (in which case the anonymous...

What is the difference between anonymous class and inner class?

A local inner class consists of a class declared within a method, whereas an anonymous class is declared when an instance is created. So the anonymous class is created on the fly or during program execution.


2 Answers

Like all objects, the anonymous inner class is eligible for garbage collected when the last reference to it no longer references it. I'm using weasel wording here because Java doesn't guarantee that things will be GC'd – the only guarantee is that it won't happen as long as there is a reference.

In this particular case, that would be when projectSettings either does a removePropertyListener() or is itself garbage collected.

Because projectSettings references the anonymous inner class and because the inner class references its containing class, that means the containing class will also live at least as long as the inner class.

like image 67
Carl Smotricz Avatar answered Oct 20 '22 17:10

Carl Smotricz


You are adding the PropertyChangeListener class you are creating to the projectSettings object. That PropertyChangeListener will not be collected as long as projectSettings references it.

like image 22
Arthur Thomas Avatar answered Oct 20 '22 19:10

Arthur Thomas