I've been messing around with Java a lot lately & I've found that for my purposes, I need several different objects to be able to communicate with & instantiate one particular class which generates a GUI JFrame with all variables, fields, etc. that the other classes need to edit. The simplest but probably erroneous way I found to do that is to instantiate the GUI class in the MainClass file, and then when I instantiate the other classes that explicitly require access to methods and variables in the GUI class, I simply pass on the GUI object to them.
My issue is, I'm not able to figure out if this just opens access to an already created object, or creates a new one entirely. In the case of the latter, then opening up several new GUI objects will just duplicate everything & waste resources with no gain whatsoever, so that's what I want to avoid.
I'll include the (haphazardly made) code below, so my question is: will this create new redundant objects, or does this just let other classes access that one instance without creating new ones?
Main Class code:
public class PlateauMainClass {
/**
* @param args the command line arguments
*/
PlateauJGui plateauGui;
GenerateGUI plateauGuiDataGen;
PlateauMainClass() {
// Instantiate the plateauGui object, then pass it into the GUI generator.
// The GUI generator will generate data for the different GUI elements.
plateauGui = new PlateauJGui();
plateauGuiDataGen = new GenerateGUI(plateauGui);
// Finally, makes the frame actually visible.
plateauGuiDataGen.makeFrameVisible();
}
public static void main(String[] args) {
// TODO code application logic here
new PlateauMainClass();
}
}
Ext. class example code:
public class GenerateGUI {
PlateauJGui gui;
GenerateGUI(PlateauJGui o) {
gui = o;
}
public void makeFrameVisible() {
gui.setVisible(true);
}
}