1

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);

}

}
Tenaar
  • 15
  • 4
  • See this http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Luis Alves Oct 29 '14 at 12:07
  • Have you done a simple reference equality check yet? – Paul Samsotha Oct 29 '14 at 12:10
  • An interesting read! Thank you =) – Tenaar Oct 29 '14 at 12:11
  • 1
    Are you trying to communicate between disparate GUI branches? You could look at Meditator Pattern : http://blue-walrus.com/2013/06/mediator-pattern-in-swing/ . Basically singleton holds reference to interesting GUI components. No need to pass GUI objects through constructors. – Oliver Watkins Oct 29 '14 at 12:21
  • @OliverWatkins To be honest, I'm actually just trying to avoid having to use `static` variables and methods in the GUI instance. The way I figure is that if the instance is already made, then there should be no point using static methods and variables since the other instances that will want to make use of these should be able to access them without through the already established GUI object, as opposed to calling static methods like `SomeClass.someMethod(someVariable);`. So uh... in short, I just need other instances to access the GUI instance without duplicating it. – Tenaar Oct 29 '14 at 12:29
  • Sergey already had the answer I needed though! So long as I'm not creating duplicate instances, all's swell. – Tenaar Oct 29 '14 at 12:29
  • @peeskillet After a quick Google search for what that means, I just learned something new which will assuage my worries =) Thanks for cuing me onto reference equality checks; I did not know you could do that! – Tenaar Oct 29 '14 at 12:37

1 Answers1

3

New object is created on every new keyword and in some exceptional cases (calling Class.newInstance method and so on). So all your objects manipulates with same instance of PlateauJGui class.

Sergii Lagutin
  • 10,561
  • 1
  • 34
  • 43
  • Oh sweet! So at least I've done it right-ish & won't risk creating instance upon instance that way. Thanks for the explanation! – Tenaar Oct 29 '14 at 12:12