Is there a Java function equivalent to var_dump()?

Asked

Viewed 1,239 times

4

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

  • There is no such thing as something ready in PHP, in Java you would have to implement it using the method Class.html#getDeclaredFields().

  • I know Java but I don’t know PHP. What would be the functionality of var_dump()?

  • This answer was useful to you?

1 answer

1

There is no equivalent in java, but I found a solution that can be useful in Soen (Link), I will translate it below:

Your alternatives are to replace the method toString() from the object to the output of its contents in a manner that suits it, or use reflection to inspect the object (similarly to what debuggers do).

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.