|
-
Apr 27th, 2010, 10:41 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Global Objects
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?
-
Apr 27th, 2010, 10:58 AM
#2
Re: Global Objects
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.
-
Apr 27th, 2010, 12:22 PM
#3
Thread Starter
Frenzied Member
Re: Global Objects
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?
-
Apr 27th, 2010, 12:38 PM
#4
Re: Global Objects
 Originally Posted by SomethinCool
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.
-
Apr 27th, 2010, 09:04 PM
#5
Thread Starter
Frenzied Member
Re: Global Objects
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?
-
Apr 27th, 2010, 11:04 PM
#6
Re: Global Objects
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.
-
Apr 28th, 2010, 07:03 AM
#7
Thread Starter
Frenzied Member
Re: [RESOLVED] Global Objects
Great.. thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|