Note: This only works when running the code as a standalone Java application, like you do with unit tests and regular applications.
if you try to do this inside a Java Applet, you will need to fiddle around with SecurityManager.
Accessing Private Fields
To access a private field you will need to call Class.getDeclaredFiled(String name) method
The methods Class.getField(String name) only return public fields.
by calling Field.setAccessible(true) just turn off the access checks for this particular Field instance, for reflection only. Now you can access it even if it is private/protected or package scope. even if the caller is not part of those scopes But you still can’t access the field using normal code which would be disallowed by compiler.
Accessing Private Methods
To access a private method you will need to call the Class.getDeclaredMethod(String name, Class parameterTypes)
The methods Class.getMethod(String name, Class parameterTypes) only return public methods