• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Singleton Class ?

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
Please explain me the meaning of Singleton class ? In lot of interviews I faced the question.. I searched on net but does not get the satisfied answer.

Thank you
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist, no more.

simply like this: final foo a=new foo();
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GOF says "Ensure a class only has one instance, and provide a global point of access to it."

most simple way to show a Singleton



use a static initializer so our "instance" will be initialized only when it is first accessed.


Or you might check if it is initialized in getInstance method.

Another solution would be to use enum as Joshua Bloch suggests in his book(Effective Java).

Enum's classes are better than the approaches using private constructors . It is more concise and provides the guarantee against multiple instantiation, even when dealing with serialization and/or reflection. (See Item 3 of Effective Java)
 
Sheriff
Posts: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Ali Khojaye wrote:use a static initializer so our "instance" will be initialized only when it is first accessed.


1) That does not compile; you can't give a final field a value twice (first null, then a Singleton object)
2) This code will be just as efficient as "private final static Singleton instance = new Singleton();". Both will create the variable only when the class is loaded first.

Or you might check if it is initialized in getInstance method.


Ah, the dreaded double checked locking "pattern". Read this to see why it should not be used in that version.

You also forgot one of the easiest ways:
These days, the locking overhead of using synchronized is not as big of a performance hit as it used to be. It's quote reasonable to use it.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Ali Khojaye wrote:use a static initializer so our "instance" will be initialized only when it is first accessed.

1) That does not compile; you can't give a final field a value twice (first null, then a Singleton object)




Correct.

Or you might check if it is initialized in getInstance method.

Ah, the dreaded double checked locking "pattern". Read this to see why it should not be used in that version.



Correct.

Thanks.
 
Something about .... going for a swim. With this tiny ad ...
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic