When debugging a C/C++ application I can create a watch of an address cast to a pointer type and then view that contents of a structure even when it goes out of scope. Is it possible开发者_运维技巧 to achieve something like that in Java?
Let's say I'm in a method, I add a watch to something like "&this" and then when I leave the class method I can still see its contents even though it is out of scope?I'm pretty sure the answer to my question is "no" so I'm mostly interested in "Why not?" explanation. Is this a JVM limitation? A JPDA limitation? Is there a better place to ask such a specialized question?
In C/C++ you are watching (the area pointed to by) an arbitrary pointer, casting to that variable.
In Java there are no pointers. You can only watch
a variable. Out of scope it does not exist.
Why would you want to watch it anyway? If you put a watch on it, it is watched properly when in scope.
You cannot watch a variable that is not in scope.
You can however watch the same object, if you find another variable that points to it.
There is no point in watching an object that is not referenced anywhere (it cannot affect the program execution in any way, it cannot be changed anymore, it will be garbage-collected and the memory reused by something completely different).
The Java-level debugging tools still work on top of the JVM memory management, so you need a valid reference to an object, you cannot just peek at random memory areas.
精彩评论