8

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);
jpmc26
30.2k14 gold badges100 silver badges152 bronze badges
asked Mar 18, 2011 at 16:40
1

6 Answers 6

3

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.

answered Mar 18, 2011 at 16:45
1
  • BCEL hmm, ugly eh? I'd rather use dump(ar1, ar2m,..) Commented Aug 4, 2017 at 18:12
3

I want to have something like: System.out.printLocals();

Also it should be great to have something like: System.out.printMembersOf(someObjectInstance);

Just about every Java class has a toString method. You override that method, and you can get a string that represents what you're asking for.

Eclipse Helios, among other IDEs, will generate a basic toString method for you.

answered Mar 18, 2011 at 17:48
2
  • this will not work for case: public void foo(int k, String s) { System.out.printLocals(); } // expected output is the same as for System.out.ptintln("k: " + Integer.toString(k)); System.out.ptintln("s: " + s); Commented Mar 23, 2011 at 12:44
  • @denys: You're correct. However, there's no reason you couldn't code a toLocalString method for each of the methods you wanted local variables. Commented Mar 23, 2011 at 12:59
1

You can use System.out.println to print out variables. Example:

int i = 42;
System.out.println("i:" + i);
answered Mar 18, 2011 at 16:44
1

OR

  • Print out to the console: (System.out.println("var"))

OR

answered Mar 18, 2011 at 16:45
1
  • On jdb there is locals. Commented Mar 5, 2015 at 7:53
0

What do you mean by "examining" ? Try a System.out.println(yourvariable), or (yourvariable.toString()) if it's an object. It'll display it in the console.

answered Mar 18, 2011 at 16:43
0

It's not simple to read the values of local variables at runtime, even if you're a debugger. If there are no debug symbols in your class, there is no way at all to see local variables, even if you use a debugger.

The most simple solution to see the values is printing them with System.out.println() or to use logging (slf4j).

If you want to example local variables at runtime without changing the code, you can try AOP (Aspect-oriented programming). Or you can use the same API that a debugger uses to examine the running VM.

answered Mar 18, 2011 at 16:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.