*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "this.moofvalue" Watch "this.moofvalue" New topic
Author

this.moofvalue

Akbar Khan
Greenhorn

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 == this.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 this.name, it's instance variable
only which is going to come in play..

We usually write this.name 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..

Regards..


rgrds,
Khushhal
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: http://aspose.com/file-tools
 
subject: this.moofvalue
 
Similar Threads
program needs to be explained
Want hepl to understand Implementin an equals()
instanceof
a doubt in hashcode program
pg no 555-K and B hashCode()