tags:

views:

3427

answers:

7

PHP has a var_dump() function which outputs the internal contents of an object, showing an object's type and content.

For example:

class Person {
  private $firstName;
  private $lastName;

  public function __construct($firstName, $lastName) {
    $this->firstName = $firstName;
    $this->lastName = $lastName;
  }
}

$person = new Person('Jon', 'Smith');
var_dump($person);

will output:

object(Person)#1 (2) {
  ["firstName:private"]=>
  string(3) "Jon"
  ["lastName:private"]=>
  string(5) "Smith"
}

What is the equivalent in Java that will do the same?

+10  A: 

It is not quite as baked-in in Java, so you don't get this for free. It is done with convention rather than language constructs. In all data transfer classes (and maybe even in all classes you write...), you should implement a sensible toString method. So here you need to override toString() in your Person class and return the desired state.

There are utilities available that help with writing a good toString method, or most IDEs have an automatic toString() writing shortcut.

serg10
+1  A: 

Your alternatives are to override the toString() method of your object to output its contents in a way that you like, or to use reflection to inspect the object (in a way similar to what debuggers do).

The advantage of using reflection is that you won't need to modify your individual objects to be "analysable", but there is added complexity and if you need nested object support you'll have to write that.

This code will list the fields and their values for an Object "o"

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

Collections already have a fairly decent toString():

Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

neu242
That is not quite what is being asked here. Whilst the java collections library classes all have robust toString implementations, Jon is looking to have a readable output from his Person class.
serg10
+4  A: 

The apache commons lang package provides such a class which can be used to build up a default toString() method using reflection to get the values of fields. Just have a look at this.

reallyinsane
updated link is http://commons.apache.org/lang/api-release/org/apache/commons/lang/builder/ReflectionToStringBuilder.html
brian_d
+3  A: 

In my experience, var_dump is typically used for debugging PHP in place of a step-though debugger. In Java, you can of course use your IDE's debugger to see a visual representation of an object's contents.

Chase Seibert
+1  A: 

I use Jestr with reasonable results.

A: 

You XML serialization, and you should get a very neat representation even of deeply nested objects.

Daniel