I know to make a singleton class but if suppose I require to clone it how can that be done.
If anybody can reply with sample code it would be better.
Thanx in advance!
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I know to make a singleton class but if suppose I require to clone it how can that be done.
If anybody can reply with sample code it would be better.
Thanx in advance!
How do you clone a non-singleton instance? What have you tried? Where exactly are you stuck?
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Not tried cloning an instance ever but I know that the class needs to implement the Cloneable interface and override the clone method of the object's class(Please correct if I am wrong anywhere). But I know of how making a class as singleton. But then suddenly this thing striked in my mind that what if I require to clone a singleton instance and if so how can I do that?
I suggest you put together a small SSCCE that demonstrates a concrete example of what you're talking about. Whether or not an instance is a singleton or not really doesn't have anything to do with how you clone it. Why you'd want to clone a singleton really depends on your context though, so I'm not sure what you're asking. An SSCCE will help with that.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
I got your context of getting confused that if suppose I am making a class singleton whats the exact reason I would like to clone it. Its just for my understanding I need to know when I make a singleton class and then I require to clone that particular class then how can I do that. Actually is it possible to clone a singleton class and if so how? Below is the demonstration.
public class A {
private static A a;
private A(){}
public static synchronized A getAObject(){
if (a == null)
a = new A();
return a;
}
}
Now i need to clone the instance of this class. How can I do that?
That's the whole objective of creating a decent singleton -- to make it so that it is difficult if not impossible to have more than one instance of the class. In this context I agree that your question doesn't make much sense.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Thanks for the answer. I tried cloning a singleton. It was exactly the same as you have told. Like the non singleton instance.