listeners and lambda method reference

sebastien deries sebastien.deries at gmail.com
Tue Feb 10 16:53:04 UTC 2015


Hi all,



While working on memory leak issues in my application, I discovered that I
had a problem with the way I use lambda reference method.

Here is the problem:



public class Lambda {

    private final Property<Boolean>       property;



    public Lambda() {



        property = new SimpleBooleanProperty();



        property.addListener(this::propertyChanged);

        property.removeListener(this::propertyChanged);



        property.setValue(true);



    }



    private void propertyChanged(ObservableValue<? extends Boolean>
notifier, Boolean previous, Boolean next) {

        System.out.println(" hello !");



    }



    public static void main(String[] args) {

        new Lambda();

    }



}



In this example, I have a property and want to listen to the property
changes. When I run this example, the console output says “hello!” whereas
I removed the listener. I thought that the syntax this::propertyChanged was
giving me a single instance of ChangeListener, but I was apparently wrong.



Is this normal?



Regards



Seb


More information about the lambda-dev mailing list