Results 1 to 7 of 7

Thread: [RESOLVED] Global Objects

  1. #1

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Resolved [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?

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    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.

  3. #3

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    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?

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Global Objects

    Quote Originally Posted by SomethinCool View Post
    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.

  5. #5

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    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?

  6. #6
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    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.

  7. #7

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    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
  •  



Click Here to Expand Forum to Full Width