Project Lombok Tutorial
How To Use @ToString Annotation With Project Lombok
@ToString
annotation enables you to avoid having to manually write a toString()
method while inspecting fields for your objects.
Using toString()
method can be an essential tool while debugging your object fields.
# Dev Environment:
- Apache maven (v3.6.1)
- maven-compiler-plugin (v3.8.1)
- lombok (v1.18.8)
- Apache Netbeans (v10.0)
TIP: For those who would like to follow along with this blog, just clone my repository git clone https://github.com/steven7mwesigwa/java-tutorials.git
and navigate to java-tutorials\project-lombok\tostring
. In there, you should be able to play around with all the source code used in this blog post.
# Terminologies
toString()
method is used to return a string representation of an object.
This method typically returns a string consisting the class name of the object, an "@" character and an unsigned hexadecimal representation of the hash code of the object. i.e com.stevenmwesigwa.tostring.demo1.Person@15db9742
.
TIP: This method can be overridden to return a more informative representation of object fields. i.e Person{firstname=Steven, lastname=Mwesigwa}
.
# Demonstration : (Without Lombok)
Let's create a Person
class that will have a manual implementation of our toString() method
. Full code here
//demo1
package com.stevenmwesigwa.tostring.demo1;
public class Person {
private String firstname;
private String lastname;
public void setFirstName(String firstname) {
this.firstname = firstname;
}
public void setLastName(String lastname) {
this.lastname = lastname;
}
@Override
public String toString() {
return "Person{" + "firstname=" + firstname + ", lastname=" + lastname + '}';
}
}
When we create and run an App.java
class that makes use of this Person class Full code here
, we successfully return a string representation of our Person object. i.e Person{firstname=Steven, lastname=Mwesigwa}
This is fine. But with @ToString
annotation from project lombok library, we don't have to manually implement toString()
methods for
our objects any more.
# Demonstration : (With Lombok)
Let's create a Person
class but this time using @ToString
annotation... . Full code here
//demo2
package com.stevenmwesigwa.tostring.demo2;
import lombok.ToString;
@ToString
public class Person {
private String firstname;
private String lastname;
public void setFirstName(String firstname) {
this.firstname = firstname;
}
public void setLastName(String lastname) {
this.lastname = lastname;
}
}
By adding @ToString
annotation, a respective toString()
method will be added for us automatically.
When we create and run an App.java
class that makes use of this Person class Full code here
, we successfully return a string representation of our Person object. i.e Person{firstname=Steven, lastname=Mwesigwa}
Now here is what's even more interesting, You're not limited to this option only. Lombok provides us other options to customize how we want to return string representations of our object fields. Let's look at some of these.
# How To use IncludeFieldNames
Configuration Key With @ToString annotation
Depending on your use case, you may not want to have field names to be part of the returned string representation of your object fields. This is what lombok.toString.includeFieldNames
configuration key is for.
By default, it's value is true . Meaning all non-static field names with their values will be returned. Setting it's value to false will make lombok not display non-static field names and simply return a list of all non-static field values separated by commas. i.e Person(Steven, Mwesigwa)
Let's create a Person
class to demonstrate this. Full code here
//demo3
package com.stevenmwesigwa.tostring.demo3;
import lombok.ToString;
@ToString(includeFieldNames=false)
public class Person {
private String firstname;
private String lastname;
public void setFirstName(String firstname) {
this.firstname = firstname;
}
public void setLastName(String lastname) {
this.lastname = lastname;
}
}
When we create and run an App.java
class that makes use of this Person class Full code here
, we successfully return a string representation of our Person object. i.e Person(Steven, Mwesigwa)
# How To Exclude Some Fields by annotating them with @ToString.Exclude
Now incase you want to exclude some fields from being returned in the string representation of our object, you can annotate them with @ToString.Exclude
.
Let's create a Person
class to demonstrate this. Full code here
//demo4
package com.stevenmwesigwa.tostring.demo4;
import lombok.ToString;
@ToString
public class Person {
private String firstname;
@ToString.Exclude
private String lastname;
public void setFirstName(String firstname) {
this.firstname = firstname;
}
public void setLastName(String lastname) {
this.lastname = lastname;
}
}
When we create and run an App.java
class that makes use of this Person class Full code here
, we successfully return a string representation of our Person object. i.e Person(firstname=Steven)
.
Note how we excluded the lastname
field from showing up.
# How To use DoNotUseGetters
Configuration Key With @ToString annotation
By default, lombok.toString.doNotUseGetters
configuration key has it's value as false . Meaning if we explicitly defined a getter
method for a non-static field to be included, it is called instead of using a direct reference of the field.
Setting lombok.toString.doNotUseGetters
to true
does the opposite.
Let's create a Person
class to demonstrate this. Full code here
//demo5
package com.stevenmwesigwa.tostring.demo5;
import lombok.ToString;
@ToString(doNotUseGetters = true)
public class Person {
private String firstname;
private String lastname;
public void setFirstName(String firstname) {
this.firstname = firstname;
}
public void setLastName(String lastname) {
this.lastname = lastname;
}
public String getFirstname() {
return "FIRST: " + firstname;
}
public String getLastname() {
return "LAST: " + lastname;
}
}
When we create and run an App.java
class that makes use of this Person class Full code here
, we successfully return a string representation of our Person object. i.e Person(firstname=Steven, lastname=Mwesigwa)
Now if you're keen enough, you will notice that because we set lombok.toString.doNotUseGetters
to true
, are direct field reference was used instead of our custom getter
methods.
Our custom getter
methods have "FIRST: " and "LAST: " text prepended to field values but this wasn't returned.
I hope you find this helpful.
Ok, this should be enough to get you comfortable with @ToString
lombok annotation.
Don't hesitate to leave a comment. Your feedback is greatly appreciated.