The toString
method is inherited by every Class in Java, from java.lang.Object
. I don't see how you can avoid not implementing (actually overriding) Object.toString
. Also, methods are defined for each class, and not for each object.
You could use a inheritance hierarchy to solve this, by having your classes inherit from a common class that overrides the default toString
implementation. This implementation may use reflection to reflect on the fields of the class (not the parent class, but actually the class for the current object), and generate the string.
You can also investigate the use of bytecode weaving if you do not want to override the method at all. Personally, I do not think that the effort invested in bytecode weaving is not worth it, especially when you could have overridden toString
.