Fxml Controller Inject Final Field

╄→尐↘猪︶ㄣ 提交于 2019-12-11 04:32:12

问题


For a fxml controller class that would map to an fxml control, such as:

public MyController
{
    @FXML
    private Button button;
}

Is it possible to declare the button field as final? If it was done, the java compiler (javac) would flag an error about that field not being initialized.

It is understood that the button field will be eventually initialized using the @FXML injection. Albeit, javac does not fully understand this, or even how it would be done.

Although the final qualifier may not be needed, however, it would be good to enforce to minimise potential mutability related errors, particularly when it is never intended to change.

Although I do not understand the complexity on how the fxml workings initialize its component, except that it uses reflection. Having said that, and generally for fields that rely on injection and reflection, then the final qualifier cannot exist on such field?


回答1:


A final field must be define at least in the constructor of the class. When using @FXML injection the fields will be injected / defined after that constructor has been called by using reflection. Therefore you can't define them as final.



来源:https://stackoverflow.com/questions/27884628/fxml-controller-inject-final-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!