1
currentColor = getCarColor(this.car.color)

Here color is private and getCarColor is a method, how do I access the variable color?

Vivek Vermani
  • 1,934
  • 18
  • 45
tarutao
  • 93
  • 1
  • 3

1 Answers1

3

You should not be accessing private variables directly: they are made private for a reason.

The proper way to do it is to add a public accessor method for the color to the car:

class Car {
    private Color color;
    // Add this method:
    public Color getColor() { return color; }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • There already is a public getter method, getCarColor (as I understand it... ), but it's not doing what I think it should be doing. – tarutao Feb 19 '14 at 16:20
  • @user3328737 You are calling it like `this.car.getCarColor()`, right? – Sergey Kalinichenko Feb 19 '14 at 16:24
  • @user3328737 You are welcome. If you no longer need help with this, consider accepting the answer to indicate to other site visitors that you are no longer actively looking for an improved answer. – Sergey Kalinichenko Feb 19 '14 at 16:43
  • I think my previous comment because of swear word... : ) I'll mark it as answered as soon as I can figure out how. – tarutao Feb 19 '14 at 16:59