Some tips for using ToString by Lombok

Tram Ho

The ToString annotation helps us to manually function toString (), a method commonly used when we want to check all the values ​​of properties in the object, it can also be a tool to help us with debugging.

To use Lombok in the Maven project, we must add dependencies:

ToString annotation Lombok

ToString annotation is a class-level annotation, placed at the beginning of the class allowing lombok to generate toString ().

By default, it will print the class name, then the name and value of each attribute in the order declared separated by commas.

For example, I create a Staff class with 2 attributes age, name. Use the ToString annotation to print the value of the staff object.

Usage IncludeFieldNames with ToString annotation

Depending on our case, it may not be possible for the names of the properties to appear in the toString () return, use IncludeFiledNames in this case.

By default, the price IncludeFiledNames is true, which means the names of all non-static properties with their values ​​will be returned in the result of the toString () function. Assigning the value IncludeFiledNames = false will cause lombok to remove the names of non-static properties in the returned result.

Reference source

Share the news now

Source : Viblo