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.

Results 1 to 8 of 8

Thread: How to clone a singleton class?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to clone a singleton class?

    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!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: How to clone a singleton class?

    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!

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to clone a singleton class?

    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?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: How to clone a singleton class?

    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!

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to clone a singleton class?

    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?

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: How to clone a singleton class?

    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.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: How to clone a singleton class?

    Quote Originally Posted by Pratyush View Post
    Now i need to clone the instance of this class. How can I do that?
    What did you try? Like I've said, you can clone it exactly how you clone any other instance.
    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!

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to clone a singleton class?

    Thanks for the answer. I tried cloning a singleton. It was exactly the same as you have told. Like the non singleton instance.

Similar Threads

  1. Question on Singleton class
    By tcstcs in forum Java Theory & Questions
    Replies: 0
    Last Post: September 26th, 2012, 07:48 AM
  2. Static class vs Singleton class
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: January 12th, 2012, 08:32 AM
  3. Subclassing Singleton class
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: December 21st, 2011, 11:42 PM
  4. Object.clone()
    By bbr201 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 14th, 2010, 10:55 AM
  5. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM

Tags for this Thread