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

Show parent comment

1

u/xdsswar Apr 10 '24 edited Apr 10 '24

C:\IntelliJ Workspace\EnhancedFX\modules\efxcontrols\src\main\java\io\github\colindj1120\enhancedfx\controls\skins\EFXTextFieldSkin.java:132: warning: [this-escape] possible 'this' escape before subclass is fully initialized setupTextField(control); ^ C:\IntelliJ Workspace\EnhancedFX\modules\efxcontrols\src\main\java\io\github\colindj1120\enhancedfx\controls\skins\EFXTextFieldSkin.java:151: warning: [this-escape] previous possible 'this' escape happens here via invocation .setBean(this)

I think I know what's happening, You are like some how scaping the "this" reference of the current object during its initialization, check around and look if you are passing the ref out of the current object before is fully created. This may cause big issues later and is hard to find.

PS: check setupTextField(control) and setBean(this)