Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lombok
java
communitycreator

What is the @ToString annotation in Lombok?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

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
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring