This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Some fun while Overriding equals 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 "Some fun while Overriding equals" Watch "Some fun while Overriding equals" New topic
Author

Some fun while Overriding equals

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

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

Joined: Sep 20, 2010
Posts: 3575
    
  14

Not really, unless you post the code that exhibits the problem you described.
ketan bhav
Greenhorn

Joined: Aug 31, 2011
Posts: 2
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

Joined: Sep 20, 2010
Posts: 3575
    
  14

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

Joined: Oct 15, 2010
Posts: 281

Stephan is correct, I don't see any particular reason there, actual code could point on some direction.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

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

Joined: Mar 10, 2010
Posts: 198

Is the above code of some help ?

as i have still not arrived at the right answer ?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Some fun while Overriding equals
 
Similar Threads
List's contains(Object o) method doesn't work for object version.
Equals
doubt in mock exam java belt
constructor - help needed
override equals