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!
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

Jennifer Elliott
Front End Web Development Techdegree Student 2,183 PointsHow to pass an argument into a constructor.
Hello,
In my code below (Task 2 of 3) I'm trying to set the private field color to the value of the argument in the constructor. However, I'm receiving an error saying that step 1 is no longer passing once I attempt to do step 2.
Step 2: *** Java *** public GoKart (String color) { private color = this.color; }
Link to code challenge: https://teamtreehouse.com/library/java-objects-2/meet-objects/add-a-constructor
1 Answer

andren
28,552 PointsThere are two issues with your code:
Access modifier (
public
,private
, etc) cannot be used within methods/constructors.this.color
is the field variable, whilecolor
is the parameter. So your assignment is doing the opposite of what it should be.
If you fix those two issues like this:
public GoKart (String color) {
this.color = color; // Removed private, and reversed order of variables
}
Then your code will work.