• Post Reply Bookmark Topic Watch Topic
  • New Topic

nesting of if.. weird....  RSS feed

 
Greenhorn
Posts: 28
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this is not getting compiled. can,t get the problem. please help.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's much easier for people to help you if you tell them what the problem is. The compiler will have given you an error message, and it would have told you where the problem occurs, so include that in your post.

It's also easier if you format your code properly. I've done that for you, and now I can see what the problem is. Was your error message something to do with not returning a value from the equals() method in all cases? Because that's exactly what the problem is. In some cases the method doesn't return a value.
 
Greenhorn
Posts: 14
BSD Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Achin Gupta wrote:

this is not getting compiled. can,t get the problem. please help.


There is a return statement missing in the case of o instanceof pr544 and ((pr544)o).ivar!=this.ivar

i.e. the return statement is missing after


The other thing is, if you overwrite the equals method you'also have to overwrite hashCode.

By definition, if two objects are equal, their hash code must also be equal. If you override the equals() method, you change the way two objects are equated and Object's implementation of hashCode() is no longer valid.


see Object as a Superclass - the hashCodeMethod
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andi Eska wrote:
Achin Gupta wrote:

this is not getting compiled. can,t get the problem. please help.


There is a return statement missing in the case of o instanceof pr544 and ((pr544)o).ivar!=this.ivar

i.e. the return statement is missing after



Hi, Andi, and welcome to the Ranch!

Please don't just give someone an answer like that. It's better to guide them toward figuring it out themselves. For more details, see LetThemDoTheirOwnHomework(⇐click) and DontBeACodeMil.l(⇐click)

The other thing is, if you overwrite the equals method you'also have to overwrite hashCode.


Small correction. The term is override, not overwrite.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!