title: How to correctly create a weak reference to method reference in Java
body: <p>I want to register a JavaFX <code>ListChangeListener</code> to an <code>ObservableList</code>. However I noticed, that under certain circumstances the Listener is not getting called.</p>
  
  <p><strong>(1)</strong> If the Listener is a method reference, everything works:</p>
  
  <pre><code>// using a direct method reference:
  private final ListChangeListener&lt;String&gt; listener = this::listDidChange;
  
  /* ... */
  public void init() {
      list.addListener(listener);
  }
  </code></pre>
  
  <p><strong>(2)</strong> If however the Listener is a weak ref to the same method, the listener is NOT invoked:</p>
  
  <pre><code>// using a weak method reference:
  private final ListChangeListener&lt;String&gt; listener = new WeakListChangeListener&lt;String&gt;(this::listDidChange);
  
  /* ... */
  public void init() {
      list.addListener(listener);
  }
  </code></pre>
  
  <p><strong>(3)</strong> Now the really funny part is, that this is working again, even though it should be the same as the previous example:</p>
  
  <pre><code>// direct method reference wrapped into a weak ref later:
  private final ListChangeListener&lt;String&gt; listener = this::listDidChange;
  
  /* ... */
  public void init() {
      list.addListener(new WeakListChangeListener&lt;String&gt;(listener));
  }
  </code></pre>
  
  <p>Two questions:</p>
  
  <ul>
  <li>What exactly happens, when a weak ref to a method ref is created?</li>
  <li>What is the difference between (2) and (3)?</li>
  </ul>