Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Singleton Class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton Class ?" Watch "Singleton Class ?" New topic
Author

Singleton Class ?

Saurabh Naik
Ranch Hand

Joined: Apr 24, 2008
Posts: 87
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


SCJP 6 (88%), SCWCD 5 (78%), preparing for SCBCD
Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
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

Joined: Apr 12, 2009
Posts: 449

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)


http://muhammadkhojaye.blogspot.com/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

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.
 
Consider Paul's rocket mass heater.
 
subject: Singleton Class ?
 
Similar Threads
how would you keep the single instance of an object over multiple JVMs
Singleton Factory Object
Doubt with a singleton class
Singletion - memory issue ?
Color choices