aspose file tools*
The moose likes Beginning Java and the fly likes Singleton Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Singleton Class" Watch "Singleton Class" New topic
Author

Singleton Class

Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
Please explain about singleton class. I know that, only on instance can be created for a singleton class. Is that correct?
Throw some light on that please......

Any sample code will be appreciated.
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
You are right in saying that a singleton class allows only one instance of that class. But it is important to know that ONE INSTANCE PER JVM. That is to say that as long as the JVM is up and running, only one instance can be used. This is used when a Class is Behavior based rather than State based.

Example.




Cheers!
Sid
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
You dont need to use the this operator with a static variable.
Example.



but if you are using in threaded environment the object need to be early loaded.



<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Harshit Rastogi wrote:You dont need to use the this operator with a static variable.

Even more important, you can't. There is no "this" in a static method / block.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10360
    
    8

If you are accessing the static variable and initializing it inside the getSingleton() method, you should be declaring it as synchronized.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Bharat Mehta
Greenhorn

Joined: Oct 29, 2007
Posts: 8
Thanks folks for your insights on Singleton Class. But I have a query. Shouldn't we override the clone method which every class inherits from the Object class in Java, to prevent any cloning of the object of our Singleton class.





Bharat Mehta
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10360
    
    8

From the API of Cloneable
Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Hi,

Do not forget the thread safety aspect !
If you are using the previous code, in a multi-threading context, then you can create several singletons.
Which is not good !

You can found interesting infos there:
webpage

Olivier
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 512
Hi,
In Singletons also make sure that the class can not be serialized.
Once serialized another instance can very well be created.
Probably make variable " singleton " as transient to prevent it from Serializing.
OR
Do not implement java.io.Serializable.

Regards,
Amit


Regards,
Amit
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

That's where the readResolve method comes in:

This method will actually replace the object that was just deserialized with the return of getInstance(). The result is, ObjectInputStream's readObject() will return the singleton instance.
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
The best way could be:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton Class