I want to declare an object in java that is like a a pointer to a pointer in C++, let me show you an example :
//*** At the application startup
//Initialize a settings container class
Settings settings = //Load settings
//Declaring static Application class that contain a reference to the settings container
Application.setSettings(settings);
//Get sub settings from settings container class
DatabaseSettings dbSettings = settings.getDbSettings();
LogSettings logSettings = settings.getLogSettings();
//Initialize components
DatabaseConnector dbConn = new DatabaseConnector(dbSettings);
Logger logger = new Logger(logSettings);
In the above code i have created a settings container class that contain all settings of my application components, then i have assigned each sub settings class to the related component, at this point during the execution of the program i want to update the settings and let the component of the application to see the updated settings, so for example during the execution i can update the settings container in this way :
//Update settings through Application static class
Settings newSettings = //assign updated settings
Application.setSettings(newSettings);
Now the problem is that when i update the settings container at runtime the application static class will contain the updated reference to the newSettings instance while each sub Settings instance keep referencing the old sub settings, so :
dbConn ---> settings.getDbSettings()
logSettings ---> settings.getLogSettings()
While i want that the two reference automatically reference to the new instance of the settings, so :
dbConn ---> newSettings.getDbSettings()
logSettings ---> newSettings.getLogSettings()
It is like a pointer to pointer … Is it possible in Java ? How can it be done ?
In Java, you only have references to objects. To have a reference to a reference you need some sort of indirection. e.g.
You can pass the ref around and change it in one place. However, this won’t notify any of the references that it has changed, just allow all references to see the same thing when they check next time.