This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes singleton? 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 » Beginning Java
Bookmark "singleton?" Watch "singleton?" New topic
Author

singleton?

Chan Apex
Greenhorn

Joined: Oct 26, 2010
Posts: 16
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
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chan Apex
Greenhorn

Joined: Oct 26, 2010
Posts: 16

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
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Oct 18, 2010
Posts: 47

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

Joined: Oct 04, 2006
Posts: 391
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

Joined: Oct 04, 2006
Posts: 391
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

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

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

Joined: Oct 04, 2006
Posts: 391
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

Joined: Oct 18, 2010
Posts: 47

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

Joined: Oct 04, 2006
Posts: 391
thanks himansu for your reply.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
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

Joined: Oct 26, 2010
Posts: 98
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

Joined: Oct 18, 2010
Posts: 47

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

Joined: Oct 13, 2005
Posts: 38025
    
  22
. . . or is it simply bad design expecting a subclass of a Cloneable class to be implementable as a Singleton?
Victor Sosa
Greenhorn

Joined: Aug 26, 2008
Posts: 20

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!


"Software development has been, is, and will remain fundamentally hard"
- Grady Booch
 
 
subject: singleton?
 
Similar Threads
garbage collection
ServletContext and properties file
Example for java doubleton pattern.
A class, which have only one object How?
JMock with singleton object