Is there any Java function equivalent to var_dump ()?

4

Does anyone know if there is any Java function equivalent to var_dump() ?

    
asked by anonymous 26.05.2015 / 01:57

1 answer

1
  

There is no equivalent in Java, but I have found a solution that can be useful in SOen ( Link ), I'll translate it below:

Your alternatives are to override the object toString() method to output your content in a way that suits you, or use reflection to inspect the object (similarly to what the debuggers do).

The advantage of using reflection is that you do not need to modify your individual objects to be "parsable," but complexity is added and if you need to support a nested object you will have to write that.

Field[] fields = o.getClass().getDeclaredFields();
for (int i=0; i<fields.length; i++)
{
    System.out.println(fields[i].getName() + " - " + fields[i].get(o));
}
    
26.05.2015 / 13:07