Results 1 to 3 of 3
- 10-06-2008, 05:41 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 31
- Rep Power
- 0
[SOLVED] Accessing private constructor
Hi,
How can I access private constructor in outside the class ?
is any possible?
Thanks in advanceLive life king size
[Lucene]
- 10-06-2008, 05:43 AM #2
Member
- Join Date
- Oct 2008
- Location
- US
- Posts
- 58
- Rep Power
- 0
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 SeekerHave fun....
JAVA FAQs
- 10-06-2008, 05:45 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
Private Classes Clarification
By justlearning in forum New To JavaReplies: 1Last Post: 05-06-2008, 10:51 PM -
Private main method
By bugger in forum New To JavaReplies: 1Last Post: 12-21-2007, 09:45 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Question of private member
By Felissa in forum Advanced JavaReplies: 2Last Post: 06-28-2007, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks