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

Lina Milns
Lina Milns
1,530 Points

Totally stuck on code challenge. Now change the color field to be private by adding the access level modifier. Help?

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

What do I need to add?

4 Answers

andren
andren
28,558 Points

Examine this line of code:

      public String mColor = "red";

The first thing written on this line is what is called an access level modifier, this denotes how the thing you are defining can be accessed, when the access level is public it means that what you define can be accessed from any class in your application.

An access level of private means that it can only be accessed from within the class it is defined in, and not any other classes in your application, there are other access levels but those two are the most common ones.

To complete this challenge you have to change the access level modifier of the mColor field to private, it currently has an access level of public, how to change it to private should be pretty self evident with the hints I just provided.

Blake Larson
Blake Larson
13,014 Points

So it's asking to change "public String mColor = "red"; " to "private String mColor = "red"; "

The private access level modifier makes the mColor variable only usable from that class.

Jamil Jones
Jamil Jones
10,852 Points

Dont forget to put your public class, which should look like this.

public class GoKart { public String mColor = "red";

}

just change it to private