• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

initillalizing an object outputs zeroes instead the values i give it

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic