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

Now change the color field to be private by adding the access level modifier.

Now change the color field to be private by adding the access level modifier.

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi mauricio,

I hope you can use my explanation:

public class GoKart {
  // the access level modifier is changed to private
  // now you can only access the mColor through the getColor method
  // This is called ENCAPSULATION and protects your variables
  private String mColor = "red";

  // this is a getter method that takes no arguments and returns the String "mColor"
  // before you created the getter your "mColor" wos public so evereone could access and use it
  // now you have the getter method to get the mColor variable through  calling the getter method
  // so it doesnt need to be public and can be changed to private
  public String getColor() {
   return mColor; 
  }
}

Grigorij