Use Reflection To build toString method : toString « Class Definition « Java Tutorial






import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Main {

  private Integer id;

  private String name;

  private String description;

  public static final String KEY = "APP-KEY";

  private transient String secretKey;

  public Main(Integer id, String name, String description, String secretKey) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.secretKey = secretKey;
  }

  public String toString() {
    return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE, true, true);
  }

  public static void main(String[] args) {
    Main demo = new Main (1, "A", "B", "C");
    System.out.println("Demo = " + demo);
  }
}








5.32.toString
5.32.1.Override toString() for Box class.
5.32.2.override the toString method in your classes
5.32.3.Use Reflection To build toString method
5.32.4.Reflection based toString() utilities
5.32.5.Use a generic toString()
5.32.6.Jakarta Commons toString Builder