Lombok @ToString example

This article will cover @ToString annotation in project lombok, its meaning, use and configuration options with examples.
Before moving further, please ensure that lombok library is installed in your IDE.

@ToString
Lombok @ToString annotation is applied over a class and it generates toString() method with all the fields of a class.

toString() method is generally overridden in a java class to define a string representation of its object.
If toString() method is not defined in a class, and you try to print an object with toString(), an output of below format is printed

com.codippa.User@150c158

A typical implementation of toString() in a non-lombok class would be

public class User {

  private String name;
  
  private String address;
  
  private int age;

  private String gender;

  // getter and setter methods

  public String toString() {
    return "[name=" + name + ", address=" + address +
         ", age=" + age + ", gender=" + gender + "]";
  }
}

Imagine toString() method when the class would have more fields, which is practically the case.

With lombok, you need to write @ToString annotation over the class and that is it.

Lombok documentation states

Generates an implementation for the toString method inherited by all objects, consisting of printing the values of relevant fields.

Above class with @ToString will look like

import lombok.ToString;

@ToString
public class User { 

  private String name; 
 
  private String address; 

  private int age; 

  private String gender; 

  // getter and setter methods
}

This will generate toString() with all the fields.
Now, if you print its object, following will be the output

LombokEntity(name=A, address=XYZ, age=22, gender=Male)

If a class contains a field which is of another user defined type and is also annotated with @ToString, then its string representation is also included in the output.

Excluding field names
Lombok @ToString annotation by default, includes field names and their values in the string representation. If you want, you can exclude names of fields and show only values.

For this, set includeFieldNames attribute to false. It is true by default. Example,

import lombok.ToString; 

@ToString(includeFieldNames = false) 
public class User { 

  private String name; 
  
  private String address; 

  private int age; 

  private String gender; 

  // getter and setter methods
}

Now, toString() will print output of the form

LombokEntity(A, XYZ, 22, Male)

Excluding fields
Lombok @ToString annotation by default, includes all fields in generated toString().
You can exclude some fields using exclude attribute as shown below.

@ToString(exclude="gender")

To exclude multiple fields, add then to an array as below

@ToString(exclude={"age", "gender"})

Remember that exclude attribute will soon be deprecated and you should use @ToString.Exclude over individual fields so that the particular field is not included in string representation. Example,

public class User {

  private String name;

  @ToString.Exclude
  private String gender;

}

Including parent fields
If a class annotated with @ToString extends a class and you want to include parent fields in the string representation of object, then add callSuper attribute as shown below

@ToString(callSuper=true)
public class User extends Person {

}

callSuper is false by default.

Changing field names
Suppose you have a field named userAddress in your java class and you want to change this name while printing its object.
With lombok @ToString, this can be done using its Include option as shown below

@ToString.Include(name="address")
private String userAddress;

This will change the name of field as it appears in toString() output.
Changing order
You can change the order in which fields of a class appear in toString() output with rank attribute of @ToString.Include.
rank will take a numeric value and the field with higher rank will come first in the generated output. Example,

import lombok.ToString; 

@ToString 
public class User { 
  @ToString.Include(rank=4)
  private String name; 

  @ToString.Include(rank=2)
  private String address; 

  @ToString.Include(rank=3)
  private int age; 

  @ToString.Include(rank=1)
  private String gender; 

}

The fields will appear in decreasing order of ranks as shown in the below output

LombokEntity(name=A, age=22, address=XYZ,  gender=Male)

Hope the article was useful.