Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this.moofvalue

 
Akbar Khan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic