The Artima Developer Community
Sponsored Link

Answer and Comment

Advertisement

Advertisement

This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Answer and Comment

Posted by Bill Venners on 01 Oct 1998, 5:48 AM

> Bill

> I am a regular reader of your Design Techniques column. It is great. Thank you for all your efforts.

> My question is that in you Worker.clone() method, why the name attribute is not copied?

I didn't bother to clone the name variable, because it holds
a reference to an immutable String object. Since no one can
change the String object, it doesn't need to be cloned. Both
Worker clones can share a reference to the same name String.

> Also I think all class should implement toString() method. My reason is very pratical. When you do debug, at any stage, by

> System.out.println(worker.toString());

> you can see the states of the instance. I have implemented the method in all my classes. I found it very helpful in my debugging process, expecially in the distributed computing envirronment.

> Looking forword to hearing your comments.

This is one I thought about. I wanted to hear what people
would say about my leaving out overriding toString(). So
far you are the only person who complained, but on the
other hand, no one has said, "Rah, rah, rah, it's about time
someone said you don't need to always override toString()."

I left it out because in most of the Java classes I have
written, I didn't override toString(), even though it
suggests it in the Java specs and javadoc documentation.
I have only overridden toString() when I was specifically
needing to look at the output of toString(), usually for
debugging purposes. That was rare, so I left it out of
the idiom mostly just based on my own experience.

So the question is, does anybody else out there have an
opinion on toString()?

bv





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us