0

I have 5-6 GUIs(seperate classes) one opens another according to their functionality, every GUI takes an object inside, I pass the final Object in a flight booking system like this to my GUI when a JButton "Book" is clicked from the seat selection menu in Book_GUI.java

Passenger_GUI frameP = new Passenger_GUI(eco.seats[x][z].getMyPassenger());
frameP.setVisible(true);    
eco.seats[x][z].setBooked(true);

in Passenger_GUI.java I have:

        private Passenger passenger_GUI;

later in Constructor without initializing passenger_GUI I assign the input Passenger Object to it:

public Passenger_GUI(Passenger myPassenger) {
    passenger_GUI = myPassenger;

Everything works no errors... I have 2 questions, do I have to initialize first passenger_GUI and then make the assigment or is it not necessary? Second important question; the changes I make in the Passenger_GU to object myPassenger(eco.seats[x][z].myPassenger), will they be saved in the previous GUI Book_GUI.java I think not because the Passenger_GUI class does not have a return value... or am I wrong?How can I make the changes I apply on an Object stable and be used for the future in Book_GUI.java?(previous GUI) Thank you for your time!

Here full classes: http://www.copypastecode.com/643018/ http://www.copypastecode.com/643022/

Anarkie
  • 657
  • 3
  • 19
  • 46
  • Please can you put here all of related code for understanding of question? – SeniorJD Jun 19 '13 at 12:59
  • @SeniorJD Updated the Question with links to source full class codes. – Anarkie Jun 19 '13 at 13:19
  • 1
    *"I have 5-6 GUIs .. one opens another according to their functionality"* 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 19 '13 at 14:51
  • @AndrewThompson ok I exaggerated I only have 3 GUIs Main_GUI, Book_GUI and Passenger_GUI, I was worried about passing the object from one to another but I see that it works and the changes are applied to the object, thanks for the links as well SSCCE looks very useful! – Anarkie Jun 19 '13 at 16:24

1 Answers1

4
  1. No, you can just initialize passenger_GUI in your constructor.
  2. The changes should persist. Since you're passing an Object and not a primitive to your Passenger_GUI constructor, Java actually passes a reference to that Passenger object. Your Passenger_GUI.passenger_GUI field should refer to the same object (and indeed, the same location in memory) as eco.seats[x][z].getMyPassenger(), so any changes made to that object from within Passenger_GUI should also be visible from Book_GUI.
jag426
  • 101
  • 5
  • I didnt understand the first answer, I put my full code in the question to the end, my Passenger class is without a constructor.So passenger_GUI is never being initialized.But still works is this valid or should I have made : `passenger_GUI = new Passenger();` `passenger_GUI = myPassenger;` ? – Anarkie Jun 19 '13 at 13:17
  • You're already doing what I said in my answer to #1. You're passing an already-existing Passenger object to your Passenger_GUI constructor: `Passenger_GUI frameP = new Passenger_GUI(eco.seats[x][z].getMyPassenger());`. Then the constructor is taking this object and assigning it to the `passenger_GUI` field. This initializes `passenger_GUI`. You don't need to also initialize it outside the constructor. – jag426 Jun 19 '13 at 13:23
  • You don't necessarily need to use `new` to initialize something. You just need to assign it to some value, which you're doing in your constructor already. – jag426 Jun 19 '13 at 13:23
  • I just made a test if the name I entered for the Passenger is visible in previous GUI and it works so all you said and I assumed was true :) thanks for clarifying!Sometimes compilers dont give errors for invalid statements... thats why I just wanted to be sure! – Anarkie Jun 19 '13 at 13:28