Trusted answers to developer questions

What is the @ToString annotation in Lombok?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

What is Lombok?

Project Lombok is a Java library that helps reduce boilerplate code. By default, Java is a verbose language. Lombok is a tool that is designed to avoid the use of repetitive code, like getters, setters, etc. Lombok reduces the boilerplate code with annotations that get plugged during the build process.

Lombok can easily be added to the project as one of the dependencies.

If the project is a Gradle project, then add the following two lines to the dependencies section of the build.gradle file:

compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'

If the project is a Maven project, then add the following two lines to the dependencies section of the pom.xml file:

<dependencies>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<version>1.18.22</version>
		<scope>provided</scope>
	</dependency>
</dependencies>

@ToString annotation

The @ToString annotation generates an implementation for the toString() method where the class name, along with each field in order — separated by commas — is printed. By default, the field names will be printed. Passing includeFieldNames=false to the annotation will print the field values but not the field names.

import lombok.AllArgsConstructor;
import lombok.ToString;

public class Main {

    static class VanillaPerson {
        private int age;
        private String name;

        public VanillaPerson(int age, String name) {
            this.age = age;
            this.name = name;
        }

        @Override
        public String toString() {
            return "VanillaPerson{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    }

    @AllArgsConstructor
    @ToString
    static class LombokPerson {
        private int age;
        private String name;
    }


    @AllArgsConstructor
    @ToString(includeFieldNames = false)
    static class LombokPersonNoFieldNames {
        private int age;
        private String name;
    }

    public static void main(String[] args) {
        VanillaPerson vanillaPerson = new VanillaPerson(15, "vanilla-sam");
        System.out.println("Vanilla Java Person - " + vanillaPerson);

        LombokPerson lombokPerson = new LombokPerson(30, "lombok-sam");
        System.out.println("Lombok Java Person - " + lombokPerson);

        LombokPersonNoFieldNames lombokPersonNoFieldNames = new LombokPersonNoFieldNames(30, "lombok-sam");
        System.out.println("Lombok Java with no field names Person - " + lombokPersonNoFieldNames);
    }
}
@ToString annotation

Code explanation

In the Main.java file, we define three different classes:

  • Line 40: VanillaPerson — The code in vanilla Java where the toString() method is implemented by the user.

  • Line 43: LombokPerson — The simplified version of VanillaPerson. This class is annotated with the @ToString annotation. Lombok generates the toString() method implementation for the user.

  • Line 46: LombokPersonNoFieldNames — The default implementation generated by Lombok includes field names of the class. This class is annotated with @ToString(includeFieldNames = false) so that the field names are not included in the string representation of the class.

RELATED TAGS

lombok
java
Did you find this helpful?