Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Meet Objects Privacy and Methods

Theo Bouwman
Theo Bouwman
2,812 Points

What am I supposed to do? Doesn't understand very wel.

What am I supposed to do?

And can someone tell what to do?

GoKart.java
public class GoKart {
  public String mColor = "red";
  public String colorOfKart = "";
}

1 Answer

Hi Theo,

You are supposed to add the getter method so that one can view the color of the cart by invoking this method. Getter methods are used to retreive private variables and similarly, by using the Setter methods one can manipulate the values of the private variables by passing in a new value. For eg:

class Animal{
private int mAge=5;       // private variable...user does not have access to it
public int getAge(){      //getter method so the user can view the age because it has private scope
    return mAge;
}
public int setAge(int age){   //setter method so that user can manipulate the age 
   mAge=age;                         
} 
}

This is all because one cannot directly access the private variables!!