Skip to main content
extended code examples
Source Link
t0r0X
  • 4.9k
  • 1
  • 45
  • 38

For a "deep" toString() there is an alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:

System.out.println("My object: " + ReflectionToStringBuilder .toString(theObject, new RecursiveToStringStyle())); // or as "toString()" overrides @Override public String toString() { return ReflectionToStringBuilder .toString(this, new RecursiveToStringStyle()); } // or @Override public String toString() { return ReflectionToStringBuilder .toString(this, new MultilineRecursiveToStringStyle()); } 

Output examples:

// RecursiveToStringStyle Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]] // MultilineRecursiveToStringStyle Person@7f54[ name=Stephen, age=29, smoker=false, job=Job@43cd2[ title=Manager ] ] 

For a "deep" toString() there is an alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:

System.out.println("My object: " + ReflectionToStringBuilder.toString(theObject, new RecursiveToStringStyle())); 

Output examples:

// RecursiveToStringStyle Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]] // MultilineRecursiveToStringStyle Person@7f54[ name=Stephen, age=29, smoker=false, job=Job@43cd2[ title=Manager ] ] 

For a "deep" toString() there is an alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:

System.out.println("My object: " + ReflectionToStringBuilder .toString(theObject, new RecursiveToStringStyle())); // or as "toString()" overrides @Override public String toString() { return ReflectionToStringBuilder .toString(this, new RecursiveToStringStyle()); } // or @Override public String toString() { return ReflectionToStringBuilder .toString(this, new MultilineRecursiveToStringStyle()); } 

Output examples:

// RecursiveToStringStyle Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]] // MultilineRecursiveToStringStyle Person@7f54[ name=Stephen, age=29, smoker=false, job=Job@43cd2[ title=Manager ] ] 
corrected typo
Source Link
t0r0X
  • 4.9k
  • 1
  • 45
  • 38

For a "deep" toString() there is aan alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:

System.out.println("My object: " + ReflectionToStringBuilder.toString(theObject, new RecursiveToStringStyle())); 

Output examples:

// RecursiveToStringStyle Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]] // MultilineRecursiveToStringStyle Person@7f54[ name=Stephen, age=29, smoker=false, job=Job@43cd2[ title=Manager ] ] 

For a "deep" toString() there is a alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:

System.out.println("My object: " + ReflectionToStringBuilder.toString(theObject, new RecursiveToStringStyle())); 

Output examples:

// RecursiveToStringStyle Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]] // MultilineRecursiveToStringStyle Person@7f54[ name=Stephen, age=29, smoker=false, job=Job@43cd2[ title=Manager ] ] 

For a "deep" toString() there is an alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:

System.out.println("My object: " + ReflectionToStringBuilder.toString(theObject, new RecursiveToStringStyle())); 

Output examples:

// RecursiveToStringStyle Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]] // MultilineRecursiveToStringStyle Person@7f54[ name=Stephen, age=29, smoker=false, job=Job@43cd2[ title=Manager ] ] 
Added link to Apache Commons Lang library.
Source Link
t0r0X
  • 4.9k
  • 1
  • 45
  • 38

For a "deep" toString() there is a alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:

System.out.println("My object: " + ReflectionToStringBuilder.toString(theObject, new RecursiveToStringStyle())); 

Output examples:

// RecursiveToStringStyle Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]] // MultilineRecursiveToStringStyle Person@7f54[ name=Stephen, age=29, smoker=false, job=Job@43cd2[ title=Manager ] ] 

For a "deep" toString() there is a alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:

System.out.println("My object: " + ReflectionToStringBuilder.toString(theObject, new RecursiveToStringStyle())); 

Output examples:

// RecursiveToStringStyle Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]] // MultilineRecursiveToStringStyle Person@7f54[ name=Stephen, age=29, smoker=false, job=Job@43cd2[ title=Manager ] ] 

For a "deep" toString() there is a alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:

System.out.println("My object: " + ReflectionToStringBuilder.toString(theObject, new RecursiveToStringStyle())); 

Output examples:

// RecursiveToStringStyle Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]] // MultilineRecursiveToStringStyle Person@7f54[ name=Stephen, age=29, smoker=false, job=Job@43cd2[ title=Manager ] ] 
Source Link
t0r0X
  • 4.9k
  • 1
  • 45
  • 38
Loading