Hacking

Hacking By Reflection: Creating Multiple instance of a Singleton class

1 Mins read

Singleton.java implements a thread safe singleton instance creation for this class. Even if multiple threads are accessing this should create only one instance of class.

package hacking;

public class Singleton {

private Singleton(){

System.out.println(“I am Singleton”);

}

private static Singleton s = new Singleton();

public static Singleton getInstance(){

return s;

}

}

Test.java file hacking the Singleton class to create multiple instances.

package hacking;


import java.lang.reflect.Constructor;


public class Test {

public static void main(String[] args) throws Exception {

Singleton s = Singleton.getInstance();

Constructor c[] = Singleton.class.getDeclaredConstructors();

c[0].setAccessible(true);

c[0].newInstance(null);

}

}

Here is the output of this program

 $  java Test

I am Singleton

I am Singleton

The private constructor is called for second time also, that means the class is not singleton anymore.

Related posts
HackingVPN

How to Access Kickass Torrents With Proxies

3 Mins read
Kickass Torrents was a popular website used by gamers, bookworms, music lovers, and movie and TV series fans worldwide. The site was…
Hacking

Hacking Higher Education: Coding Bootcamps and Workforce Preparedness

3 Mins read
Post-pandemic, the labor shortage that many businesses large and small are bemoaning actively represents the single greatest source of opportunity for workers…
AmongUSGamingHacking

What Are The Hacks in AmongUs App?

4 Mins read
What is AmongUs Video Game? Inspired by The Thing, the science-fiction horror film and Mafia, the live party game, Marcus Bromander, the…
Power your team with InHype

Add some text to explain benefits of subscripton on your services.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *