Reviewed my copy of Effective Java and you're right, but if you read
all of chapter 3, you'll find a variety of references to the broken-
ness of this pattern. Its a mystery to me as to why v2 doesn't mention
getClass() != other.getClass() anywhere, but none of that changes the
simple fact that using instanceof without then also making 'equals'
final is a code bug.

On Oct 17, 11:22 pm, grydholt <[email protected]> wrote:
> On Oct 17, 10:45 am, Reinier Zwitserloot <[email protected]> wrote:
>
> > I never said Josh changed his views. I said version 1.0 contained a
> > bug. Which that second snippet clearly shows: Using instanceof like
> > that, _unless_ some documentation explicitly states that cross-type
> > equality is intended (and thus also locks down any subclasses from
> > adding equality-relevant state), cannot result in a transitive equals
> > method.
>
> Okay from your earlier post:
>
> > > > Simple enough. But wrong. In the second edition, the instanceof check
> > > > was revised to this:
>
> > > > if (o.getClass() != this.getClass()) return false;
>
> From this I concluded that you wanted to say that Bloch recommended
> "instanceof" in the first edition and getClass() in the second
> edition. This is not true since his Point example in the second
> edition is:
>
> ----
> public class Point {
>     private final int x;
>     private final int y;
>     public Point(int x, int y) {
>         this.x = x;
>         this.y = y;
>     }
>
>     @Override public boolean equals(Object o) {
>         if (!(o instanceof Point))
>             return false;
>         Point p = (Point)o;
>         return p.x == x && p.y == y;
>     }
>
>     ...  // Remainder omitted}
>
> ----
>
> As readers of the book will know Bloch uses composition in order to
> handle new value components.
>
> Kind regards,
> Jacob Grydholt

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to