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

Help please! I can't get pass this error: Bummer! Ensure that you assign the value passed into the constructor to this.

As mentioned I can't seem to get pass that error. I'm stumped. Any help would be much appreciated. Thanks

GoKart.java
public class GoKart {
  private String mColor = "red";

  public GoKart(String color){
    mColor=color;

   }
  public String getColor() {
    return mColor;
  }
}

2 Answers

xiaoluke
xiaoluke
6,387 Points

I believe in the new Java Objects course (it was recently refreshed) you use instead :

private String color = "red";

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

this.color refers to the color of the object and color is the value passed in via the constructor.

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

Luke is right, this.color = color; will be frivate cuz u cant really call it, its assigned jsut to the construcor.

P.S. Luke, try using ``` java before u put your code in, it will make it shiny and ez to read :) look

class GoKart {
  private String color;

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

  public String getColor() {
    return color;
  }

}

Solved thank you both!