Home Lombok Lombok @ToString examples

Lombok @ToString examples

1. Lombok @ToString basic example

The Lombok @ToString annotation generates an implementation for the toString method and by default, it’ll print your class name, along with each field, in order, separated by commas. By default non-static fields are excluded in generated toString().

Make sure you already installed Lombok setup for your IDE. To Setup in Eclipse or in Spring Tool Suite refer to our Lombok Maven example setup with Eclipse.

Lomboked LombokToStringDemo1.java

@ToString
public class LombokToStringDemo1 {
	
	private Long id;
	
	private String name;
	
	private static boolean defaultStatus = false;

}

DeLomboked LombokToStringDemo1.java

public class LombokToStringDemo1 {
	private Long id;
	private String name;
	private static boolean defaultStatus = false;

	@Override
	public String toString() {
		return "LombokToStringDemo1(id=" + this.id + ", name=" + this.name + ")";
	}
}

2. Skip field names

Declaring annotation like @ToString(includeFieldNames = false) will skip the field names including in toString().

Lomboked LombokToStringDemo2.java

@ToString(includeFieldNames = false)
public class LombokToStringDemo2 {
	
	private Long id;
	
	private String name;

}

DeLomboked LombokToStringDemo2.java

public class LombokToStringDemo2 {
	private Long id;
	private String name;

	@Override
	public String toString() {
		return "LombokToStringDemo2(" + this.id + ", " + this.name + ")";
	}
}

3. Exclude fields from toString method

By default static fields are excluded from generated toString(), two different ways fields can be excluded, using Lombok @ToString annotation at class level and field level.

3.1. Exclude at class level

Following example demonstrates how to exclude fields from generated toString() at class level.

Lomboked LombokToStringDemo3.java

@ToString(exclude = { "id", "dateOfBirth" })
public class LombokToStringDemo3 {

	private Long id;

	private static boolean defaultStatus = false;

	private String name;

	private LocalDate dateOfBirth;

}

DeLomboked LombokToStringDemo3.java

public class LombokToStringDemo3 {
	private Long id;
	private static boolean defaultStatus = false;
	private String name;
	private LocalDate dateOfBirth;

	@Override
	public String toString() {
		return "LombokToStringDemo3(name=" + this.name + ")";
	}
}

3.2. Exclude at field level

Declaring @ToString.Exclude at field level will exclude them from toString().

Lomboked LombokToStringDemo4.java

@ToString
public class LombokToStringDemo4 {

	private Long id;

	private static boolean defaultStatus = false;

	@ToString.Exclude
	private String name;

	@ToString.Exclude
	private LocalDate dateOfBirth;

}

DeLomboked LombokToStringDemo4.java

public class LombokToStringDemo4 {
	private Long id;
	private static boolean defaultStatus = false;
	private String name;
	private LocalDate dateOfBirth;

	@Override
	public String toString() {
		return "LombokToStringDemo4(id=" + this.id + ")";
	}
}

4. Include only specific fields/methods to toString method

Lombok provides several ways to include only specific fields/methods to generate toString method.

4.1. Include fields/methods

Lombok @ToString by default uses non-static fields to generate toString method. Additionally to include specific method we have to declare @ToString.Include on top of method. Note that only instance (non-static) methods that take no arguments can be included. 

Lomboked LombokToStringDemo5.java

@ToString
public class LombokToStringDemo5 {

	private Long id;

	private static boolean defaultStatus = false;

	@ToString.Exclude
	private LocalDate dateOfBirth;
	
	@ToString.Include
	public String getDOB() {
		return this.dateOfBirth.
				format(DateTimeFormatter.ofPattern("dd-MMM-yy"));
	}
}

DeLomboked LombokToStringDemo5.java

public class LombokToStringDemo5 {
	private Long id;
	private static boolean defaultStatus = false;
	private LocalDate dateOfBirth;

	public String getDOB() {
		return this.dateOfBirth.
				format(DateTimeFormatter.ofPattern("dd-MMM-yy"));
	}

	@Override
	public String toString() {
		return "LombokToStringDemo5(id=" + this.id + ", getDOB=" + this.getDOB() + ")";
	}
}

4.2. Include only specific fields

There are two ways we can include only specific fields to toString() like following 2 examples.

4.2.1. Example using onlyExplicitlyIncluded attribute :

Lomboked LombokToStringDemo6.java

@ToString(onlyExplicitlyIncluded = true)
public class LombokToStringDemo6 {

	private Long id;

	@ToString.Include
	private String name;
	
}

DeLomboked LombokToStringDemo6.java

public class LombokToStringDemo6 {
	private Long id;
	private String name;

	@Override
	public String toString() {
		return "LombokToStringDemo6(name=" + this.name + ")";
	}
}

4.2.2. Example using of attribute :

Lomboked LombokToStringDemo7.java

@ToString(of = {"id", "status"})
public class LombokToStringDemo7 {

	private Long id;
	
	private String name;
	
	private boolean status;
	
}

DeLomboked LombokToStringDemo7.java

public class LombokToStringDemo7 {
	private Long id;
	private String name;
	private boolean status;

	@Override
	public String toString() {
		return "LombokToStringDemo7(id=" + this.id + ", status=" + this.status + ")";
	}
}

Note : As per Lombok documentation using of and exclude attributes with @ToString will soon be marked as deprecated, they are old style of inclusion/exclusion of fields in generating toString method using Lombok.

5. Lombok @ToString callSuper

By setting callSuper to true, you can include the toString method of your superclass in the generated method. You can do this only when your superclass also generating toString method using Lombok @ToString, otherwise it uses Object class default toString() implementation.

Lomboked Rectangle.java and Box.java

@ToString
public class Rectangle {
	
	private double width;
	
	private double length;

}
@ToString(callSuper = true)
public class Box extends Rectangle {

	private double height;
}

DeLomboked Box.java

public class Box extends Rectangle {
	private double height;

	@Override
	public String toString() {
		return "Box(super=" + super.toString() + ", height=" + this.height + ")";
	}
}

6. doNotUseGetters

Normally, if getters are available, they are called instead of using a direct field reference. This behavior can be changed using @ToString(doNotUseGetters = true). You can also change this globally by using Lombok configuration using configuration property lombok.toString.doNotUseGetters = [true | false] (default: false)

Lomboked LombokToStringDemo8.java

@ToString(doNotUseGetters = true)
@Getter
public class LombokToStringDemo8 {

	private Long id;
	
}

DeLomboked LombokToStringDemo8.java

public class LombokToStringDemo8 {
	private Long id;

	@Override
	public String toString() {
		return "LombokToStringDemo8(id=" + this.id + ")";
	}

	public Long getId() {
		return this.id;
	}
}

7. Changing order and change name for fields

You can change the name used to identify the member and you can change the order in in which the members are printed with name and rank attributes. Members without a rank are considered to have rank 0, members of a higher rank are printed first, and members of the same rank are printed in the same order they appear in the source file.

Lomboked LombokToStringDemo8.java

@ToString
@Getter
public class LombokToStringDemo9 {

	@ToString.Include(name = "userId", rank=1)
	private Long id;
	
	@ToString.Include(name = "userName", rank=2)
	private String name;
	
	private boolean status;
	
}

DeLomboked LombokToStringDemo8.java

public class LombokToStringDemo9 {
	private Long id;
	private String name;
	private boolean status;

	@Override
	public String toString() {
		return "LombokToStringDemo9("
				+ "userName=" + this.getName()
				+ ", userId=" + this.getId()
				+ ", status=" + this.isStatus() + ")";
	}

	public Long getId() {
		return this.id;
	}

	public String getName() {
		return this.name;
	}

	public boolean isStatus() {
		return this.status;
	}
}

8. Conclusion

In this guide we have covered how to use Lombk ToString annotation with different options. You can refer Delombok Maven example to see how looks like lombok generated code for your Lomboked classes.

You can checkout source code at github.

You might be interested in our other following Lombok Tutorials :

  1. Lombok @Getter @Setter and lazy getters
  2. Lombok @EqualsAndHashCode
  3. Lombok @AllArgsConstructor
  4. Lombok @NoArgsConstructor
  5. Lombok @RequiredArgsConstructor
  6. Lombok @Accessors
  7. Lombok @Data
  8. Lombok @Value
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories