I have a method and want to examine variables inside it without debugging - is it possible in Java?
I do not want to write tons of code like:
System.out.println("a: " + a);
I want to something like:
System.out.printLocals();
Also it should be great to have something like:
System.out.printMembersOf(someObjectInstance);
Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference. VariableName.
To declare a variable in Java, all that is needed is the data type followed by the variable name: int numberOfDays; In the above example, a variable called "numberOfDays" has been declared with a data type of int. Notice how the line ends with a semi-colon.
Initialize the variable outside a block, and set it inside the block. You will have access to it outside. Save this answer.
Well, you can write a method with a varargs parameter and just write:
dump(variable1, variable2, variable3, variable4, ...);
It's not ideal, but it will be enough in some circumstances. There's no way to automatically grab all the local variables from a method and dump them though.
You might consider some sort of bytecode manipulation (e.g. with BCEL) which could do it... but it would be pretty ugly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With