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

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,075 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 :-)