• Post Reply Bookmark Topic Watch Topic
  • New Topic

initillalizing an object outputs zeroes instead the values i give it  RSS feed

 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and when i uses it in another class named "Check" it returns zeroes :

Output : the colors are (0,0,0)
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because of variable shadowing in the overloaded constructor of Check2.
The assignments in that constructor assign the parameters to themselves, instead of to the instance variables, as you intended.
This will become apparent when you declare the constructor parameters as final, like so:

It will not, however fix the problem. How do you think you could or should fix this?
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:This is because of variable shadowing in the overloaded constructor of Check2.
The assignments in that constructor assign the parameters to themselves, instead of to the instance variables, as you intended.
This will become apparent when you declare the constructor parameters as final, like so:

It will not, however fix the problem. How do you think you could or should fix this?


change the names . e.x (int rr, int gg , int bb)
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, that's definitely one way to go.
Another would be to make use of the this keyword, which let you keep the identical names and prevent shadowing:


More on this.
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:Sure, that's definitely one way to go.
Another would be to make use of the this keyword, which let you keep the identical names and prevent shadowing:


More on this.


yes i tried it.
it gives me the default value output: (0,0,0)
i will will check again when i get back from work
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!