File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Class

 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are accessing the static variable and initializing it inside the getSingleton() method, you should be declaring it as synchronized.
 
Bharat Mehta
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way could be:

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic