regarding ReflectionToStringBuilder.toString(this);

722 views Asked by At

The following method is to implement toString

public String toString() {
return ReflectionToStringBuilder.toString(this);
}

How to understand the parameter this used by toString, what does it stand for here? Besides, why do we need to implement toString this way?

2

There are 2 answers

0
Reimeus On

ReflectionToStringBuilder is a utility class used to generate the String returned when overriding Object#toString using the fields of the class instance passed as an argument.

this here refers to current instance of the Object whose fields are used to generate the returned String

The main advantage to using this class is that the String does not have to be generated every time fields are added or removed.

0
Martin Wachocki On

In Java the keyword this keyword is used as a reference to the current object.