• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

SINGLETON METHOD

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do we need to make instance variable as static?
Why do we need to make instance variable as private?
Why do we need to mark constructor with private?
Why do we need to make instance variable as private?
Why do we need to mark method with static, synchronized?
Please brief me with when to use and where to use with 3 or 4 examples?
If somebody has already used this concept, please put some insight into it.

public class SingleTon
{
private static SingleTon instance;

private SingleTon()
{

}


public static synchronized SingleTon getSingleTonInstance()
{
if(instance ==null)
{
instance = new SingleTon();
}

return instance;
}
}
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do we need to make instance variable as static?
- static => class level. All instances share the same copy.
Had it not been static, every instance created of this class would have been composed of it's own private instance.
Why do we need to make instance variable as private?
- what if it is public? it's value can then be modified. The reference could be made to point to null by some malicious code. Result- creates another object on the next call to the same method.
Why do we need to mark constructor with private?
- what if otherwise? instances of this class can be created by invoking the constructor. No more a singleton.
Why do we need to make instance variable as private?
- this is a trick question. you just asked it few questions back. gotcha.
Why do we need to mark method with static, synchronized?
- "static synchronized". Obtains a lock on the instance of class java.lang.Class that represents this singleton class at runtime, when this static synchronized method is invoked. The idea is to try & ensure that only one instance is created. For ex: imagine 2 threads trying to create a singleton and the context switches just after the 1st thread finshes the if predicate. Both threads end up creating an instance - not a true singleton.
 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please brief me with when to use and where to use with 3 or 4 examples?
If somebody has already used this concept, please put some insight into it.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Singleton pattern is very common. I'm sure you can use Google to find all the information you need. Also, you should get your hands on a copy of Design Patterns. It explains Simpleton and many other basic design patterns with all the detail you need. It also provides very good examples.
HTH
Layne
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic