What are the shortcut to Auto-generating toString Method in Eclipse?

If you use lombok they have a @ToString annotation which will generate the toString for you.

The reason why this is much better to use instead of generating toString with eclipse for instance is that if you later add,remove or change attributes of the class, you will also have to regenerate the toString. If you use lombok you don't have to do that.


I personally like to implement a toString method for all objects, as it helps in debugging.

I would look into using the Apache Commons ToStringBuilder.

You can implement a simple toString method using reflection as follows:

public String toString() {
   return ToStringBuilder.reflectionToString(this);
}

Using this method, you will not have to update your toString method if/when fields are added.


Eclipse 3.5.2 (and possibly earlier versions) already provides this feature. If you right-click within the editor, you'll find it under Source -> Generate toString()...

To answer your question about whether it's a bad practice to autogenerate toString(), my opinion is that it is not. If the generated code is very similar to the code you would have written yourself, then why bother typing it out?