• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • 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.
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic