Jakarta Commons toString Builder : Class Helper « Apache Common « Java

Jakarta Commons toString Builder

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);


Related examples in the same category

1.Implement hashCode using commons-lang
2.Use CompareToBuilder class to create compareTo method for your own class
3.Use Reflection To build toString method
4.Implement equals method using commons-lang