|
Page 4 of 5 Generate the toString() Method
Methods like equals(), toString(), and hashCode() are used on a regular basis. However, when it comes to actually writing implementations for these methods, not only are most of us reluctant to do that but we are also not sure how exactly and easily to write them. The builder package provides utility classes that can help you easily create
implementations for these methods. In most cases it just takes one line of code. We will look at the toString capabilities of Lang.
The toString() method
You might have noticed in Listing 4 that even if we just pass an object of java.util.Date to System.out.println(), the output we get is a proper display of the date and time. This is possible because when you just pass an object reference, the toString() method gets called automatically. So in our example we are essentially calling the toString() method of the java.util.Date class and the proper output we get is because someone has taken the trouble to override the toString() method from the java.lang.Object class in the java.util.Date class.
If the toString() method is not overridden, the output you get is just the name of the class and the hashcode. No data in the class will get displayed. So if you have written a new class and wish to get a proper output on print, you need to override the toString() method in your class.
Listing 6. The toString() method
public class Computer {
String processor;
String color;
int cost;
/** Creates a new instance of Computer */
public Computer(String processor, String color, int cost) {
this.processor=processor;
this.color=color;
this.cost=cost;
}
public static void main(String[] args) {
Computer myComp=new Computer("Pentium","black",1000);
System.out.println(myComp);
}
public String toString(){
return ToStringBuilder.reflectionToString(this);
/*
return ToStringBuilder.reflectionToString(this
, ToStringStyle.SHORT_PREFIX_STYLE);
return ToStringBuilder.reflectionToString(this
, ToStringStyle.MULTI_LINE_STYLE);
return new ToStringBuilder(this)
.append("processor", processor).toString();
*/
}
}
Listing 6 shows a Computer class that has three fields in it. The toString() method is what is special. The call to the reflectionToString() method is able to figure out which are the fields in the class, and then prints their name and value.
In the main() method, we simply create an instance of the class and
then print it. The output in this case is dev2dev.Computer@f6a746[processor=Pentium,color=black,cost=1000].
So if you don't wish to put too much effort into it and yet require toString() implementations for your classes, there is no easier way than to copy and paste these two lines of code into all your classes. If you wish to have more control over what is generated, look at the commented options. You can apply various styles to the output or even build the entire output by creating a new instance of ToStringBuilder. The output, if you were to execute each of the four return statements in the listed order, is shown in Listing 7.
Listing 7. The four possible outputs based on the ToStringBuilder method used
1) dev2dev.Computer@f6a746[processor=Pentium,color=black,cost=1000]
2) Computer[processor=Pentium,color=black,cost=1000]
3) dev2dev.Computer@f6a746[
processor=Pentium
color=black
cost=1000
]
4) dev2dev.Computer@192d342[processor=Pentium]
|