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

Help with constructors Java. I'm really struggling with this code challenge.

I cannot do this coding challenge at all. I think I have a complete misunderstanding. Can anyone tell me what the answer is and WHY that is the answer?

The question is: Now in the body of your constructor, set the private field color to the value of the color argument passed into the constructor. (Make sure you are setting the right color*)

class GoKart { private String color = "red";

public GoKart(String color){ private this.color = color; }

public String getColor() { return color; }

}

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,023 Points

You don't have to add the private identifier to the this.color. You only add the identifier when creating the variable itself not when calling the variable.

So your code should end up looking like this:

class GoKart { 
private String color = "red";

public GoKart(String color){ this.color = color; }

public String getColor() { return color; }

}

Thanks so much. Your answer forced me to re-read the question and I now understand how I was doing it wrong. I appreciate you taking the time to answer :-)