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 Meet Objects Add a Constructor

Manik Mehra
Manik Mehra
1,877 Points

Now in the body of your constructor, set the private field color to the value of the color argument passed into the cons

I am not ABLE TO FIGURE OUT the problem in my code.....Please help.

Thank you

GoKart.java
public class GoKart {
     private String mColor = "red";
  private String mColor = "red";
     public GoKart(String color) {
    mColor = color;
  }
}

4 Answers

Hi Manik,

Kirill is correct, to add to his answer though; your code declares and assigned mColor twice, although the Challenge doesn't ask for a variable called mColor to be created at all. You'll want to use this to clarify the color variable scoped to the method vs the color variable scoped to the class; this in a method references the object whose context the method is running in.

class GoKart {
  private String color = "red";

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

}

Hope that helps!

Kirill Dakhnyuk
Kirill Dakhnyuk
7,073 Points

Hi, change mColor to color, then in constructor you need to use RIGHT color which you declare in private field. You need to use THIS. So... public GoKart (String color) { this.color = color; }

Yakir Haviv
PLUS
Yakir Haviv
Courses Plus Student 1,117 Points

class GoKart { private String color = "red";

public String getColor() { return color; } public color(String color) { this.color = "red"; }

}

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

public String getColor() { return color; }

}