With Lombok
01 import lombok.ToString;
02
03 @ToString(excludes="id")
04 public class ToStringExample {
05 private static final int STATIC_VAR = 10;
06 private String name;
07 private Shape shape = new Square(5, 10);
08 private String[] tags;
09 private int id;
10
11 public String getName() {
12 return this.getName();
13 }
14
15 @ToString(callSuper=true, includeFieldNames=true)
16 public static class Square extends Shape {
17 private final int width, height;
18
19 public Square(int width, int height) {
20 this.width = width;
21 this.height = height;
22 }
23 }
24 }
Vanilla Java
01 import java.util.Arrays;
02
03 public class ToStringExample {
04 private static final int STATIC_VAR = 10;
05 private String name;
06 private Shape shape = new Square(5, 10);
07 private String[] tags;
08 private int id;
09
10 public String getName() {
11 return this.getName();
12 }
13
14 public static class Square extends Shape {
15 private final int width, height;
16
17 public Square(int width, int height) {
18 this.width = width;
19 this.height = height;
20 }
21
22 @Override public String toString() {
23 return "Square(super=" + super.toString() + ", width=" + this.width + ", height=" + this.height + ")";
24 }
25 }
26
27 @Override public String toString() {
28 return "ToStringExample(" + this.getName() + ", " + this.shape + ", " + Arrays.deepToString(this.tags) + ")";
29 }
30 }