I think I just wrote the single worst toString(), ever!
public String toString() {
if (this != null)
return “**** implement me”;
return “i am null”;
}
Heck, it’s late Friday evening. I’m in a hurry…. Get over it…. I will obviously fix this, but I’m still disgusted I’m using it.
Can you throw out something worse?
Ok, back to work.
You should check out the commonclipse plugin for eclipse. It’s free, and it will automatically generate toString, hashCode, compareTo, and equals methods for you. Very handy if you’re doing Spring/Hibernate stuff. (Be careful though you can run into some problems if any of those methods make use of lazily-loaded collections.)
return new ReflectionToStringBuilder(this).toString()
Thanks for the tip Jason. I saw Commonclipse mentioned on an AppFuse tutorial I was working on that brought about this crazy toString.
Good stuff Tony. I’ll have to compare that to what Commonclipse can do.
Erik
It’s hard to get worse, seeing as how if this != null was to fail, the toString() call would’ve thrown an NPE, meaning you’d never see “i am null”