• Post Reply Bookmark Topic Watch Topic
  • New Topic

Whats wrong in below code - toString returning Null  RSS feed

 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



output is
name is nulland salary is0
name is nulland salary is0
name is nulland salary is0
name is nulland salary is0



expected output :

Name is Crish Salary: 2000
Name is Tom Salary: 2400
Name is Ram Salary: 3000
Name is John Salary: 6000


Please tell me where i going wrong
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 36 and 37 are the wrong way around. You're assigning the values of the member variables to the argument variables instead of the reverse.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in your constructor in lines 36 and 37.
If name refers to the argument of a constructor and this.name to the instance variable of the class, what do you think this assignments do?

Other issues.
1. Your Empl class should implement Comparable, not Comparator.
2. Do not compare ints in a way you do in line 60. This might result in an overflow and incorrect value. Use Integer.compare instead.
3. you should give your classes and variables meaningful names. So your class should be Employee, not Empl. Your sal variable should be salary etc.
4. Is employee with null name and 0 salary a valid one? If not, you do not need non-argument constructor.
 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:The problem is in your constructor in lines 36 and 37.
If name refers to the argument of a constructor and this.name to the instance variable of the class, what do you think this assignments do?

Oh that was a blunder...

it worked fine.

Thanks
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't lived until you have made that mistake. Everybody does it. Just as everybody puts a semicolon after if and wonders why their if block doesn't seem to workAnd there are lots of other mistakes everybody makes. You mustn't worry about it.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:1. Your Empl class should implement Comparable, not Comparator.

And it should be called Employee.

Don't make things cryptic just to save yourself a few keystrokes. Suppose you need to add an "Employer" class as well; what does that become: Emplr?

Winston
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Pawel Pawlowicz wrote:1. Your Empl class should implement Comparable, not Comparator.

And it should be called Employee.

I already said that :P
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!