Results 1 to 3 of 3
  1. #1
    piyu.sha is offline Member
    Join Date
    Oct 2008
    Posts
    31
    Rep Power
    0

    Question [SOLVED] Accessing private constructor

    Hi,

    How can I access private constructor in outside the class ?

    is any possible?

    Thanks in advance
    Live life king size
    [Lucene]

  2. #2
    kzvi.kzvi.1 is offline Member
    Join Date
    Oct 2008
    Location
    US
    Posts
    58
    Rep Power
    0

    Default

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

    code:

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

    code:

    public class Test {

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

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

    c[0].setAccessible(true);

    c[0].newInstance(null);

    }

    }

    Here is the output of this program

    I m hacked

    Here are few more similar hacking tricks based on same technique of setAccessible() method call.

    Examples of
    Object Hacking Java demonstrates how a Singleton can be accessed, how a private field can be accessed using same technique.
    ____________________________________________
    Software Wiki | Interview FAQs | Lucene Search | Oracle | ORM | Struts2 | Job Seeker
    Have fun....
    JAVA FAQs

  3. #3
    piyu.sha is offline Member
    Join Date
    Oct 2008
    Posts
    31
    Rep Power
    0

    Default

    Thanks a lot, this is perfect, I cant believe this.
    Live life king size
    [Lucene]

Similar Threads

  1. Private Classes Clarification
    By justlearning in forum New To Java
    Replies: 1
    Last Post: 05-06-2008, 10:51 PM
  2. Private main method
    By bugger in forum New To Java
    Replies: 1
    Last Post: 12-21-2007, 09:45 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Question of private member
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •