views:

114

answers:

2

this was an interview question posed to me..I vaguely answered it uses Java reflections..but I was not sure. how does that work?

+6  A: 

It uses the Java Debugger which provides commands to do just that:

jdb print myObj.myInstanceField

Back in the old days there were really people doing this on a command line! :)

NOTE: To display local variables, the containing class must have been compiled with the javac -g option.

dube
+5  A: 

The key to your question is almost certainly java.lang.reflect.AccessibleObject, which allows the debugger to turn off the access control checks and poke around. Spring uses the same mechanism to get access to the variables for dependency injection.

Donal Fellows