Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singleton?

 
Chan Apex
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know about singleton object i.e. creating a single instance and reusing that object again and again...but how can we stop it from cloning...?javascript:emoticon('');
 
Henry Wong
author
Marshal
Pie
Posts: 21363
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maybe you should do a quick search on the net, or even in these forums. There are tons of questions about singletons, and many of them have code.

Maybe you can figure out how from such codes.

Henry
 
Chan Apex
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

class single{
private single(){ }
single ob;
public static single getInstance()
{
if(ob==null)
ob=new single();
return ob;
}
public static void main(String ...a)
{
getInstance();
}

Upto this ok! but how to stop cloning..

I already spend so much time in searching for it...but it doesn't clarify..that's i posted my doubt..

please clear it.
Thanks

 
Henry Wong
author
Marshal
Pie
Posts: 21363
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apex han wrote:
Upto this ok! but how to stop cloning..

I already spend so much time in searching for it...but it doesn't clarify..that's i posted my doubt..


First of all, you have to make the variable private and static. Second, you have to make getInstance() synchronized.


As for your question... have you actually tried to clone the singleton? You'll see that you can't. Objects are not by default clonable.

Henry
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apex han wrote:Upto this ok!

Not quite. Note that the getInstance method is not thread-safe. If it is called from multiple threads simultaneously, it's possible that more than one instance is created (although only one will be referenced, and the others thus become liable to be garbage-collected).

but how to stop cloning..

Override clone() and throw a CloneNotSupportedException.
 
himanshu.harish agrawal
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As correctly said by Henry the object are not by default clonable. So, if you will try to clone the object you will immediately get CloneNotSupportedException.
So, not to worry about cloning, if you do not want your class to support cloning.
But if you want then Override clone() and throw a CloneNotSupportedException.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very Simple implement Cloneable interface and override clone() method of class Object. Will this not allow to make cloning of your Singleton class Object.


i hope this code would help you !
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
himanshu.harish agrawal wrote:As correctly said by Henry the object are not by default clonable. So, if you will try to clone the object you will immediately get CloneNotSupportedException.
So, not to worry about cloning, if you do not want your class to support cloning.
But if you want then Override clone() and throw a CloneNotSupportedException.


few of the objects are as default clonable like Vector.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from throwing an exception, it's also allowed to return "this". The Javadoc of Object.clone() does not forbid this:
The general intent is that, for any object x, the expression: will be true, and that the expression: will be true, but these are not absolute requirements.
So returning "this", thereby letting x.clone() == x, is allowed.

But the easy way is of course to simply not implement Cloneable at all.
 
himanshu.harish agrawal
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat Ranjan wrote:
himanshu.harish agrawal wrote:As correctly said by Henry the object are not by default clonable. So, if you will try to clone the object you will immediately get CloneNotSupportedException.
So, not to worry about cloning, if you do not want your class to support cloning.
But if you want then Override clone() and throw a CloneNotSupportedException.


few of the objects are as default clonable like Vector.


Yes Prabhat, but all user-defined objects will by default never support cloning for the security purpose.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is fine, no userdefined classes are by default implements clonable.

But if i haven't overriden the clone() method and implemented Clonable Interface.

It means my class is safe and no one can make copy of it, correct.
 
himanshu.harish agrawal
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat Ranjan wrote:that is fine, no userdefined classes are by default implements clonable.

But if i haven't overriden the clone() method and implemented Clonable Interface.

It means my class is secure no one can make copy of it, correct.


If that was a question then Yes, you are safe ..
Reiterating: No user defined class (whether abstract, concrete, inner, singleton, factory etc..) is by default clonable.

Thanks.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks himansu for your reply.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a singleton, you could also simply define an enum with just one constant:



This works fine because you can specify arbitrary fields and methods in java enums.
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
himanshu.harish agrawal wrote:
Prabhat Ranjan wrote:that is fine, no userdefined classes are by default implements clonable.

But if i haven't overriden the clone() method and implemented Clonable Interface.

It means my class is secure no one can make copy of it, correct.


If that was a question then Yes, you are safe ..
Reiterating: No user defined class (whether abstract, concrete, inner, singleton, factory etc..) is by default clonable.

Thanks.


But what if your singleton class happens extends another class that does implement Cloneable and does support cloning? Or perhaps an abstract Cloneable superclass that doesn't provide the clone() implementation? (though admittedly extending such a class as a singleton would seemingly make little sense as the author would be telling you that any concrete subclasses should be Cloneable) Your only choice then it would seem would be to override and either return 'this' or else throw the CloneNotSupportedException.
 
himanshu.harish agrawal
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Hurtt wrote:
himanshu.harish agrawal wrote:
Prabhat Ranjan wrote:that is fine, no userdefined classes are by default implements clonable.

But if i haven't overriden the clone() method and implemented Clonable Interface.

It means my class is secure no one can make copy of it, correct.


If that was a question then Yes, you are safe ..
Reiterating: No user defined class (whether abstract, concrete, inner, singleton, factory etc..) is by default clonable.

Thanks.


But what if your singleton class happens extends another class that does implement Cloneable and does support cloning? Or perhaps an abstract Cloneable superclass that doesn't provide the clone() implementation? (though admittedly extending such a class as a singleton would seemingly make little sense as the author would be telling you that any concrete subclasses should be Cloneable) Your only choice then it would seem would be to override and either return 'this' or else throw the CloneNotSupportedException.


If a class is extending a clonable class then it will automatically inherit the clone() method of that class so no need of overriding but that sub class will be forced to become a clone supporting class.

Or is any way to prevent that class from not becoming clone supporting class, apart from throwing some exception or restructuring the classes to prevent cloning.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or is it simply bad design expecting a subclass of a Cloneable class to be implementable as a Singleton?
 
Victor Sosa
Greenhorn
Posts: 20
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing you may want to be careful (if apply) is the serialization issue. You can serialize your singleton object and then recreate it and this will breakdown your implementation (cause you now will have two singleton objects ). To solve this you can simply write the readObject method and force this "new" instance to reuse your actual singleton, this way you will always have one singleton object in your classpath.



Hope this may work for you. Cheers!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic