• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

singleton?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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('');
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 47
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks himansu for your reply.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76887
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . or is it simply bad design expecting a subclass of a Cloneable class to be implementable as a Singleton?
 
Greenhorn
Posts: 20
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
And then the flying monkeys attacked. My only defense was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic