Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Class ?

 
Saurabh Naik
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • 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();
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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)
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic