Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,364 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