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


How to pass an argument into a constructor.


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

28,558 Points

There are two issues with your code:

  1. Access modifier (public, private, etc) cannot be used within methods/constructors.

  2. this.color is the field variable, while color 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.