• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

static member in singleton pattern

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is why static member variable is used in singleton pattern?
In my project there is one class called as IDMConfig.The code of it is as follows.

public class IDMConfig {

private static IDMConfig obj;

private IDMConfig() {
init();
}
private void init(){
System.out.println("init method");
}

public static IDMConfig getInstance() {
if (obj == null)
obj = new IDMConfig();
return obj;
}

}

Object of IDMConfig is called from various classes in application like the following
IDMConfig.getInstance();

but init method which is called inside constructor..This method is only calling at the first time.If we logout the application and login then also this method is not calling?? and if we do the restart of websphere then only this method is calling next time.Can anybody explain me why this might be happening?
why member variable here is declared as static ?what is the use of this?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instance of IDMConfig.getInstance() returns you an object which is created only once for the entire application you"re using. When you declare static the variable or method gets called at the compile time itself.when you build & compile your classes or when you restart your server you can find the contets of the method.
Hope this clarifies your Questions
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
That is simply how a singleton works. The behaviour you describe is what you would expect of a singleton, it is constructed only once through the running life of your application, you only get one instance. If this behaviour is not the right thing for your class maybe the singleton pattern was not the right choice, or maybe you need to have a public init method to invoke on it, etc.
The static member, the private constructor, and the code in the getInstance() method are the mechanism to implement the singleton pattern. That is how you assure that you will not instantiate more than one for this class.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be really picky, this assures you have only one instance per class loader. If you run in a complex environment with multiple class loaders, you might get multiple copies of your singleton or any other static variable.
 
They weren't very bright, but they were very, very big. Ad contrast:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic