• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

this.moofvalue

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic