Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes this.moofvalue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "this.moofvalue" Watch "this.moofvalue" New topic


Akbar Khan

Joined: Jun 13, 2007
Posts: 24
K & B page no 526

public boolean equals(Object o) {
if ((o instanceof Moof) && (((Moof)o).getMoofValue()
== this.moofValue)) {
return true;
} else {
return false;

//(Moof)o).getMoofValue() == this.moofValue))

K & B page no 535

public boolean equals(Object o) {
SaveMe test = (SaveMe)o;
if (test.y == y && test.x == x) { // Legal, not correct
return true;
} else {
return false;

// if (test.y == y && test.x == x)

WHY NOT ( test.y == this.y)

K & B PAGE NO 563

public boolean equals(Object o) {
if((o instanceof Dog) &&
(((Dog)o).name == name)) {
return true;
} else {
return false;

// (Dog)o).name == name)

WHY NOT ( (Dog)o).name ==
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi Akbar Khan

It's because there is no local variable with name namewhich is going to shadow the instance vartiable name..

Hence you write name or, it's instance variable
only which is going to come in play..

We usually write to prevent the shadowing of instance
variable by local variable..

Also, it's a good practice to refer the instance variable by
keyword this even though, there is no local variable with the same name to shadow it..


Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Akbar Khan:
if (test.y == y && test.x == x)

The problem here is that x is transient. You may have unexpected behaviour if you use serialization.

(Dog)o).name == name)

You are comparing the object references of the strings and not the content. As result, something like this will not have the expected result.

[ June 26, 2007: Message edited by: Manfred Klug ]
I agree. Here's the link:
subject: this.moofvalue
It's not a secret anymore!