• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some fun while Overriding equals

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


I have a piece of code

My question comes from equals method in the block


I had a Myclass object which did not have a testValue associated with it now when i ran the test i got an Invocation exception . This example is in customized form so it might not throw exception . However, in the real code we had to use a getter in the code block as:-



I could not understand why this worked because fields for the calling object are same in either case . CAn anyone explain ?
 
Stephan van Hulst
Bartender
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really, unless you post the code that exhibits the problem you described.
 
ketan bhav
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Stephan. It's perfect one right?

Cause in below case

if(this.getTestValue() == myClass.getTestValue()){
return true;
}

whatever return by method getTestValue is it self of type Object.
 
Stephan van Hulst
Bartender
Pie
Posts: 5590
55
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class Ragi posted is broken for several reasons, but none of them would cause it to throw an "invocation exception".

Unless Ragi posts the real code with the class that "did not have a testValue associated with it", there's nothing we can do to help.
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan is correct, I don't see any particular reason there, actual code could point on some direction.
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the late reply .

The code goes as follows in the getTestValue() the code is as follows:-



so as i could understand the getter is responsible for removing the InvocationException in equals method which was thrown at line


When i changed the equals as follows:-




It works and no exception is thrown . In either case , the value of fields of MyClass should be same then why is the behaviuor different on invocation of this.testValue and this.getTestValue()

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the above code of some help ?

as i have still not arrived at the right answer ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic