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

  • SEARCH
  • Home
  • 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 8283981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:56:07+00:00 2026-06-08T10:56:07+00:00

I was doing research on singleton and I have developed a very basic singleton

  • 0

I was doing research on singleton and I have developed a very basic singleton class..

public class SingletonObject {
    private static SingletonObject ref;
    private SingletonObject () //private constructor
    { }

    public  static synchronized   SingletonObject getSingletonObject()
    {
        if (ref == null)
            ref = new SingletonObject();
                return ref;
        }


    public Object clone() throws CloneNotSupportedException
    {throw new CloneNotSupportedException ();
    }   
}

Now below is the one way which I have cracked the singleton..

public class CrackingSingleton {

     public static void main(String[] args) throws ClassNotFoundException,
       IllegalArgumentException, SecurityException,
       InstantiationException, IllegalAccessException,
       InvocationTargetException {

        //First statement retrieves the Constructor object for private constructor of SimpleSingleton class.
        Constructor pvtConstructor = Class.forName("CrackingSingleton.SingletonObject").getDeclaredConstructors()[0];
        //Since the constructor retrieved is a private one, we need to set its accessibility to true.
        pvtConstructor.setAccessible(true);
        //Last statement invokes the private constructor and create a new instance of SimpleSingleton class.
         SingletonObject  notSingleton1 = ( SingletonObject) pvtConstructor.newInstance(null);
         System.out.println(notSingleton1.hashCode());
         System.out.println("notSingleton1 --->"+notSingleton1.toString());
         SingletonObject  notSingleton2 = ( SingletonObject) pvtConstructor.newInstance(null);
         System.out.println("notSingleton2 --->"+notSingleton2.hashCode());
         System.out.println(notSingleton2.toString());
    }
}

Please advise other ways also by which the singleton can be cracked..!!

  • 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-06-08T10:56:10+00:00Added an answer on June 8, 2026 at 10:56 am

    Three ways I can think of are:

    Serialization

    If your singleton class is serializable, then you could serialize an instance of it, and deserialize it back and get a second object of that class.

    You could avoid this by implementing readResolve method.

    public class Singleton implements Serializable {
       private static final Singleton INSTANCE = new Singleton();
    
       public static Singleton getInstance(){
           return INSTANCE;
       }
    
       public Object readResolve() throws ObjectStreamException {
            return INSTANCE; //ensure singleton is returned upon deserialization.
       }
    }
    

    Class Loading

    The same class could be loaded by two different class loaders, as such, you could create two instances of your singleton class by simply invoking its getInstance method in a class loaded by two different class loaders. This approach would work without having to resort to violating the private constructor.

    ClassLoader cl1 = new URLClassLoader(new URL[]{"singleton.jar"}, null);
    ClassLoader cl2 = new URLClassLoader(new URL[]{"singleton.jar"}, null);
    Class<?> singClass1 = cl1.loadClass("hacking.Singleton");
    Class<?> singClass2 = cl2.loadClass("hacking.Singleton");
    //...
    Method getInstance1 = singClass1.getDeclaredMethod("getInstance", ...);
    Method getInstance2 = singClass2.getDeclaredMethod("getInstance", ...);
    //...
    Object singleton1 = getInstance1.invoke(null);
    Object singleton2 = getInstance2.invoke(null);
    

    Reflection

    As you have well pointed out, via reflection you could create two instances of the class. I think the previous examples was just a variant of the same approach. But I believe you could prevent these two from happening using a SecurityManager.

    System.setSecurityManager(new SecurityManager());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been doing research on HTML canvas libraries and I came across this
I am doing research on CUDA programming. i have the option to buy a
I've been doing research on compilers. The lexer seems to be very straight forward:
I have been doing research, and I havent found a way to remove the
I am doing research on a subject. The sites which are developed in javascript
I have been looking around online, doing research into how to use blocks. I
After doing research I have discovered that Google Maps (on a webpage) will not
I have been doing research for a few months now on the possibility of
I have a very basic question. I want to retrieve a zip code automatically
A very specific question, so doing research on it is kind of hard. It

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.