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

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!