Jakarta Commons toString Builder : toString « Class Definition « Java Tutorial






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

public class Main {
  private String id;

  private String firstName;

  private String lastName;

  public Main() {
  }

  public String toString() {
    return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("id", id).append(
        "firstName", firstName).append("lastName", lastName).toString();
  }

  public static void main(String[] args) {
    Main example = new Main();
    example.id = "1";
    example.firstName = "First Name";
    example.lastName = "Last Name";

    System.out.println("example = " + example);
  }
}








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