Call any instance method of your object from the leak constructor thison this mathod. This may be OK, as long as this method is under your control (is not publicly available), and you make sure that you do not miss thisfurther from it. Using thisas an argument for any method is, of course, a more explicit option, and this happens when you speak x.addEventListener(this). Perhaps more intriguing, since the less obvious way to leak thisis to not use thisthe argument itself, but an instance of an internal / local / anonymous class, say
public class Main
{
private class MyListener extends MouseAdapter { ...}
public Main() {
class Listener1 extends MouseAdapter { ... }
someSwingComponent.addMouseListener(new MyListener());
someSwingComponent.addMouseListener(new Listener1());
someSwingComponent.addFocusListener(new FocusAdapter() { ... });
}
}
this , . , , static, .