Java Magazine, September/October 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 24 java 9 access all types in the exposed package and all members within those types including private members via setAccessible as in earlier Java versions For more information on setAccessible and reflection see Oracles documentation Dependency injection Reflection is commonly used with dependency injection One example of this is an FXML based JavaFX app See Define Custom Behavior in FXML with FXMLLoader in this issue for more on using FXML Ed When an FXML app loads the controller object and the GUI components on which it depends are dynamically created as follows First because the app depends on a controller object that handles the GUI interactions the FXMLLoader injects a controller object into the running app that is the FXMLLoader uses reflection to locate and load the controller class into memory and to create an object of that class Next because the controller depends on the GUI components declared in FXML the FXMLLoader creates the GUI controls declared in the FXML and injects them into the controller object by assigning each to the controller objects corresponding @ FXML instance variable In addition the controllers event handlers that are declared with @ FXML are linked to the corresponding controls as declared in the FXML Once this process is complete the controller can interact with the GUI and respond to its events We use the opens to directive to allow the FXMLLoader to use reflection on a JavaFX app in a custom module Modularized Welcome App In this section we create a simple Welcome app to demonstrate module fundamentals We create a class that resides in a module provide a module declaration compile the module declaration and Welcome class into a module run the class containing main in that module At compile time if multiple modules have the same name a compilation error occurs At runtime if multiple modules have the same name an exception occurs
You must have JavaScript enabled to view digital editions.