PDA

Click to See Complete Forum and Search --> : [RESOLVED] Global Objects


SomethinCool
Apr 27th, 2010, 10:41 AM
Is it possible to instantiate an object and use it across all classes in a java application?

For instance, if I instantiated an object and created a session to a server, could I store this object somewhere and access the methods and data anywhere across all classes in the application?

kfcSmitty
Apr 27th, 2010, 10:58 AM
Not sure if this is what you're looking for, but you may want to look into the singleton pattern.

http://en.wikipedia.org/wiki/Singleton_pattern

What it does, in a nutshell, is limits an object to one instance. So matter where or how many times you instantiate an object, it will always point to the same object.

SomethinCool
Apr 27th, 2010, 12:22 PM
So for instance...

I have a JFrame (SplashFrame.java) that calls a method in a class (AdminClass.java) that initializes a session with a server and creates a new object (objSession). Once the initialization is complete, the Main app JFrame loads (MainFrame.java). Also, I want to be able to access the session object from other classes as well, so I don't want to just pass the object from the splash frame to the main frame.

From the Main JFrame, would I be able to access objSession by using this singleton method, or am I looking for something different?

kfcSmitty
Apr 27th, 2010, 12:38 PM
So for instance...

I have a JFrame (SplashFrame.java) that calls a method in a class (AdminClass.java) that initializes a session with a server and creates a new object (objSession). Once the initialization is complete, the Main app JFrame loads (MainFrame.java). Also, I want to be able to access the session object from other classes as well, so I don't want to just pass the object from the splash frame to the main frame.

From the Main JFrame, would I be able to access objSession by using this singleton method, or am I looking for something different?

You could access it using the singleton method, but you would still have to create a new instance of it (which would reference the already instantiated instance of it) to access it.

All singleton does is ensure that only one instance of the object ever exists. If you want to have methods available to every function in a particular program, but still allow multiple instances, it sounds like you would want to use an interface and have your classes implement it.

SomethinCool
Apr 27th, 2010, 09:04 PM
Alright... Makes sense. Is this singleton method a workaround or is this an acceptable programming method? Also, from a performance standpoint, is this acceptable as well?

kfcSmitty
Apr 27th, 2010, 11:04 PM
Singleton is a design pattern for coding. It is an accepted programming method if you require only one instance of an object.

From a performance standpoint, technically you're saving memory and time since you don't have to instantiate the object over and over and only one instance of it is ever kept in memory.

SomethinCool
Apr 28th, 2010, 07:03 AM
Great.. thanks for your help.