• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

Java Loop all declared variables & their values


I want to loop all local and global variables that are declared in a jsp.
How can i acces all these variables and print out their 'String' value ?

String var1 = "value1";
String var2 ="value2";
int var3 = 15;
int var4 = 16

output :
var1   15
var2   value2
5 Solutions
you can't get the local( inside method ) variables but can do class/instance/member variables using reflection.
If you are trying to debug a jsp, in that case you should put the sysouts in spots where you want to see the variable values, otherwise like ksivananth says you will not be able to access the local variables, only  instance member will be visible - which  you can view via the toString() method if you have overridden that.
You shouldn't have any Java code inside a JSP, so there should be nothing to "loop".

There's no way to "loop all variables" in Java anyway.
Or rather there is but it requires a lot of very tricky code and won't do what you want it to (nothing will).
Murali MurugesanFull stack Java developerCommented:
if you could tell us the reason why you are trying to iterate the local and global variables , we could suggest any alternatives.

Here is a description of how to use JDPA to debug variables:
 Perhaps that helps.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now