Forums Register Login

Override Equals

+Pie Number of slices to send: Send


When we try to run this code, there is 2 compile time errors generate

TestOne.java:13: error: cannot find symbol
if((o instanceof TestOne ) && ( (TestOne)o.getTestValue() == thi
s.getTestValue() )){
^
symbol: method getTestValue()
location: variable o of type Object
TestOne.java:13: error: incomparable types: TestOne and int
if((o instanceof TestOne ) && ( (TestOne)o.getTestValue() == thi
s.getTestValue() )){
^
2 errors
+Pie Number of slices to send: Send
I assume you're wondering why you're getting an error?

The dereference (dot) operator has higher precedence than the cast operator does. This means the compiler thinks you're trying to call getTestValue() before you're casting o to TestOne.

Aside from that, you shouldn't have statements of the form
Instead, do
Note your class does not conform to Object's contract. You're not overriding the hashCode() method in accordance with the equals() method.
+Pie Number of slices to send: Send
thanks mr. Stephan van Hulst , now my program is compiled successfully.......
Do not threaten THIS beaver! Not even with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 687 times.
Similar Threads
Generics and collections K&B pg 526
Doubt about instanceof operator
Some fun while Overriding equals
Accessing private variables in equals()
instanceof
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:42:52.