Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5993037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:37:04+00:00 2026-05-22T23:37:04+00:00

While we are creating a class using Singleton Pattern I am able to clone

  • 0

While we are creating a class using Singleton Pattern I am able to clone in the same class.
It means the Singleton Pattern rule was violated. Can any one please clarify my doubt.
My Code is:

class SingletonObject implements Cloneable
{
    private SingletonObject()
    {
        // no code req'd
    }

    public static SingletonObject getSingletonObject()
    {
        SingletonObject obj = null;
      if (ref == null){
          // it's ok, we can call this constructor
          ref = new SingletonObject();  

          try {
              obj = (SingletonObject)ref.clone();
            if(obj.equals(ref)){
                System.out.println("both objects are same");
            }else{
                System.out.println("both objects are not same");
            }
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

      }    
      return obj;
    }

    private static SingletonObject ref;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:37:05+00:00Added an answer on May 22, 2026 at 11:37 pm

    Good observation. Your code is a good example of why many Java developers consider clone() to be problematic:

    It is a mechanism which circumvents many restrictions that you can place on the creation of objects. It can thus be used to duplicate singletons. This is (arguably) a design flaw of Java. The only solution is to not use clone, or not to implement a public clone() method in your class.

    In practice it’s not much of a problem, because clone will only work if the class explicitly implements it, which you would not do for a singleton. It’s still ugly though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while creating a blog i am using the following model class and form .but
While creating classes, I followed OO conventions and declared all class variables before using
I'm using this php class ( http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php ) for creating the different sized images
in this answer he said that i can use RIA services while creating my
I am getting this error while creating a public method on a class for
I am creating a class in MATLAB and while I have little experience with
While creating a repository by using NHibetnate(or, whatever), should I keep explicit BeginTransaction(), Commit(),
I am new to Hibernate. While creating a small app using it I got
While creating a file synchronization program in C# I tried to make a method
While creating classes in Java I often find myself creating instance-level collections that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.