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

Challenge 2 task 2 'Now change the color field to be private by adding the access level modifier.'

i have attempted the question many times with this code but i always comes with syntax errors.

GoKart.java
public class GoKart { public String mColor = "red";
                     public String getColor() {
                       return mColor; }
                    } 
public class GoKart { private String mColor = "red";
                     public getColor() { return mColor; }
                     public setColor(String color){ mColor = color;}

3 Answers

Ricky Sparks
Ricky Sparks
22,249 Points

public class GoKart { private String mColor = "red"; public String getColor() { return mColor; } }

Ricky Sparks
Ricky Sparks
22,249 Points

add String to getColor and erase the last line of code it is not needed public setColor(String color){ mColor = color;}

Examples and answers to the exercises would be helpful, and save everyone a lot of time!

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

The instructions are asking just for the color field to be marked private (instead of public).

Other code is looking pretty good, you just got ahead of the challenge ;)

Hope it helps!