Is there any convension which says when should I use it for my custom objects? In model layer or in view?
If your question is 'When should I override the default toString() method and use create my own?' then there is one set of answers. If your question is 'when should I call the toString()
method (implicitly like "This is my object: " + myobj
or explicitly)?' then there is a different set of answers.
So, when should you override the toString()
? From the Javadoc for Object:
In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
I strongly agree with this statement. Every custom class you create should override the toString method, and should find a way to textually represent the class content in a meaningful (but not necessarily exhaustive) way. For example, a 'Point' class representing a co-ordinate x
and y
where x==3 and y==4 should return something like Point(3,4)
. On the other hand, I believe that some classes are over-exhaustive with toString, like ArrayList does too much. I would prefer ArrayList(size 10: [sample0 .... sample9])
. Edit: The Java tutorial for Object
also says the same thing about toString():
The Object's toString() method returns a String representation of the object, which is very useful for debugging
The reasons for making every class have a toString is because of the times you should use the toString() method.... and, as far as I am concerned, there are only three places in general programming where using toString() is legitimate (and two 'special' cases):
- In exception handling: throw new IllegalStateException("Failed to process " + value);
- for logging program progress using things like log4j.
- when the JavaDoc for the toString() Method for the class you are using defines a specific, and non-alterable format for the result. e.g. String.toString() or Integer.toString(). If the class you are using does not specify what the toString() returns, and does not guarantee that it won't change, then create your own way to present the data.
There are two special cases for using toString()
:
- I think, even though this is a special case, that this is the most important reason: when running your Java program through a debugger like the one built in to Eclipse. When you are stepping through your program the debugger will use the
toString()
method to show you the state of all your variables. This is where, in many cases, you will value the toString() more than anything else.
- when developing or debugging a program it is often convenient for you to add 'println' statements, and they can be things like: System.out.println("Processing customer " + customer);`
In summary, the toString() method should produce enough data to debug problems with the object content. It is used occasionally for a 'contractual' representation of a value (like String.toString()
).
But, in general, toString() Is a tool for the PROGRAMMER, not for the user. It is there to assist you when things go wrong, not when they go right.
In my real Job I file bugs against code (and pursue the bugs relentlessly) which throws exceptions where the exception has an Object.toString()
in the message. I also mentally +1 people who's programs produce good detail during exceptional conditions.
Now, in your particular situation, I think that you should use the toString() method to represent the state of your Customer, and, because the display-conditions for your customer are different to the internal state (e.g. display needs to be UPPERCASE,) I think that you should have a different mechanism for displaying the Customer than the toString() method.
In other words, the information you will need to debug a situation involving a Customer (something printed in a stack trace or debug session) is different than the information you want to display on the screen.