My IDE (NetBeans) type checks my Collections
while I am typing code. But then, why do I have to cast the returned object of Object.clone()
? Which is fine. No harm no foul. But still, I don't understand.
Is type checking, without casting, the returned object of Object.clone()
not possible? The generics framework makes me think the IDE could check the type of object references on the right-side of the "=" mark without casting while I am typing? I don't get it.
addendum
My usage case was just that I had a private Calendar
field, pubdate. I was going to write:
Calendar getPubdate() {
return pubdate;
}
but there is a risk that the invoker could modify my pubdate, so I returned a copy:
Calendar getPubdate() {
return (Calendar) pubdate.clone();
}
Then, I wondered why I needed to cast pubdate.clone()
. The method signature has the type right there. NetBeans should be able to figure that one out. And NetBeans seemed to be doing something similar with regard to Collections
.
MyObject
fromclone()
rather thanObject
- this removes this entire issue. It is further recommended never to useclone()
(Effective Java Item #11).this.clone()
on programmer object, especially at Wed's night after Tue's release. Sorry but I have to write this comment..Why can't smart IDE just fixes all bugs for us LOL