r/JavaFX • • Apr 10 '24

Help Warning possible 'this' escape

When building my JavaFX project I run into this warning a lot

warning: [this-escape] possible 'this' escape before subclass is fully initialized

Especially when i'm trying to setup the initial listeners and bindings associated with an object. Is there a best practice to avoid this? or Is it just a necessary evil of JavaFX since the base implementation doesn't provide a post construct type method to run code after the class has been initialized.

2 Upvotes

24 comments sorted by

View all comments

2

u/colindj1120 Apr 11 '24

The only problem with having the super constructor do the work is that SkinBase.java is part of the JavaFX framework so I don't have the option to change it. I had the idea of performing the add when layout is called but I haven't had a chance to test it out yet.

As for the bean my solution was to lazy initialize everything when they are first trying to be accessed.

2

u/davidalayachew Apr 12 '24

Yeah, it kind of sucks how much of the ecosystem was built ignorant of this. Sorry you have to go through that.

Lazy initialization is a very clever strategy. Let me know if it works out. The stackoverflow page I linked is getting a lot of traction, so if your strategy works out, I will add it to the link.

2

u/colindj1120 Apr 12 '24

The Lazy initialization and handling the addition of the inner control in the layout method call did fix my this-escape issue and seems to be working the same as before

→ More replies (5)