Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Some fun while Overriding equals

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Saloon Keeper
Posts: 13500
305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really, unless you post the code that exhibits the problem you described.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 13500
305
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the above code of some help ?

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