Thursday, August 16, 2012

Java object Describer instead of toString() method

Consider you have a JavaBean/DTO/VO, It has some properties. Sometime at runtime we are trying to print object's properties's value with property name, just for debugging purpose.

In this situation, most of the time we override the toString() method of Object class and for the shake of convenience, we just use the object name in the print method.

If you notice carefully, you have to write code for that feature. The solution is to create some generic solution for that, so that that can be used everywhere.

I have created an Interface named Describable and it has one menthod named describe().







Also, created an abstract class named ValueObject that implements Describable and Serializable interface.



 Test program would be given below:




















This way you can reduce your duplicate coding and this is used by almost all java class.

If you wish to use the utility, you can download the binary and use.