-3

So I have a code which is like:

Class A

Class B

methodB(frame initialization)

Class C

methodC(frame initialization)

An object of ClassA calls ClassB and then a frame is initialized. Now when a button on ClassB is pressed an object of ClassC is initialized.

When a button on ClassC is pressed I need to reinitialize an object of ClassA with updated parameters. So while reinitializing an object of ClassA from ClassC, the older object of ClassC is still there.

How do I exit from the old object of ClassA and initialize a new object of ClassA when a particular button is pressed? I am able to create a new instance of ClassA but the problem is in exiting from the previous object from ClassC because if I add System.exit(0) in classC, it closes both objects of ClassA. I just want the old object to close.

fejese
  • 4,601
  • 4
  • 29
  • 36
The_Lost_Avatar
  • 992
  • 5
  • 15
  • 35
  • If you use singleton, you will only have 1 instance of A. So, if you initialize it twice, the first instance will be replaced by the second one. – AliBZ Oct 17 '13 at 20:18
  • Use class constructor... – Stanislav Mamontov Oct 17 '13 at 20:18
  • @AliBZ : Singleton is a great option. – The_Lost_Avatar Oct 17 '13 at 20:29
  • @StanislavMamontov : Can you elaborate ?? – The_Lost_Avatar Oct 17 '13 at 20:29
  • Your question does not make any sense, you should first become clear what a class is, what an object is and what a method is. You dont call a "Class", you call a method of an object (which is an instance of some class). And you dont initialize a class (but possibly you instantiate/initialze an object instance). – Gyro Gearless Oct 17 '13 at 20:32
  • @GyroGearless : Thanx, I have tried to update. – The_Lost_Avatar Oct 17 '13 at 20:47
  • @MAD_ABOUT_JAVA you created too much frames on runtime, see CardLayout, then every hockey described in your question will be/are/ could be Background or Workers threads, your descriptions is not clear, mentioned ..... – mKorbel Oct 18 '13 at 06:14
  • `System.exit()` terminates the currently running Java Virtual Machine. So basically, your entire program shuts down. That's why it closes both objects. – hfontanez Jan 10 '15 at 15:10

1 Answers1

2

Edit: You can't do this. You can't reinitialize this or overwrite an instance globally by reconstructing it.

You need to keep a reference to the existing class A instance. When creating an instance of class B, make it accept class A in the constructor and pass this. The same when creating class C. Now if you have proper getters, you can, in C, call this.getB().getA().performSomeUpdate() whre performSomeUpdate updates the A instance to new parameters by setting fields.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • I am doing this thing only . The problem I am facing is that there is a GUI initialized in B with previous parameters. The parameters are updated but they would be used only after restart . – The_Lost_Avatar Oct 17 '13 at 20:27
  • Remember, this is also dependent upon what types of objects ClassA, ClassB, and ClassC are. If they are some kind of internal frame (for instance), you can hide them and make them visible; each action producing a GUI event that your code could listen to and do stuff when these events take place. In essence, you could "reinitialize" your internal frames via the listeners. Of course, this is just one example. – hfontanez Jan 10 '15 at 15:14