you all know very well java does'nt allow you to access private variables and methods outside the class.However ther is some loopholes in java "TO DO THIS".
consider a sample class:
private.java
public class privatedata {
private void message()
{
System.out.print("This message was derived from private data");
}
private void message(String data)
{
System.out.print("your message is"+data);
}
}
now am accessing private method from outside class "LETS ROCKZZZZ"
import java.lang.reflect.*;
public class accssingprivatedata {
public static void main(String args[])
{
try
{
Class c=Class.forName("privatedata");
Object obj=c.newInstance();
Method m=c.getDeclaredMethod("message", null);
m.setAccessible(true);
m.invoke(obj, null);
}
catch (Exception e) {
// TODO: handle exception
}
}
}
consider a sample class:
private.java
public class privatedata {
private void message()
{
System.out.print("This message was derived from private data");
}
private void message(String data)
{
System.out.print("your message is"+data);
}
}
now am accessing private method from outside class "LETS ROCKZZZZ"
import java.lang.reflect.*;
public class accssingprivatedata {
public static void main(String args[])
{
try
{
Class c=Class.forName("privatedata");
Object obj=c.newInstance();
Method m=c.getDeclaredMethod("message", null);
m.setAccessible(true);
m.invoke(obj, null);
}
catch (Exception e) {
// TODO: handle exception
}
}
}
Không có nhận xét nào:
Đăng nhận xét