What are the potential issues I need to look out for when using reflection. I am very confused in reflection, Why java provide this type of functionality to access private data member.
Private : Like I'd think, only the class in which it is declared can see it.
Then Why it is possible to access private things in other class? this terminology (reflection) completely overflow my concept of private(Access Specifier) properties in java.
I visited many links about this topics but not given complete explanation about this topics. eg:
package example;
import java.lang.reflect.Method;
class A{
private void privateMethod(){
System.out.println("hello privateMethod()");
}
}
class B{
public static void main(String[] args) throws Exception {
A d = new A();
Method m = A.class.getDeclaredMethod("privateMethod");
m.setAccessible(true);
m.invoke(d);
}
}
So please explain scenario about this approach in depth? I need advantage and disadvantage of private methods accessibility in other class?