Hacking Java class: Calling private Constructor of Java class outside the class

Here is a sample code by which you can hack and call the private constructor of a class using reflection.

public class HackMeIfYouCan {
        System.out.println("I m hacked");       


public class Test {

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

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





Here is the output of this program

I m hacked

So if you want your java class to be protected make sure you dont give ReflectPermission to your class.

Post a Comment Default Comments

Individuals who comment on FromDev at regular basis, will be rewarded in Top Commenter section. (Comments are selectively moderated so please do not spam)