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

Logan Detering
Logan Detering
1,953 Points

I cant get past this step, please help

Please help

GoKart.java
class GoKart {
  private String color = "red";

  public Gokart(String color){
    color = red;

  }

  public String getColor() {
    return color;
  }

}

3 Answers

Joseph Frazer
Joseph Frazer
5,404 Points

When you defined color you made it a string. Line 5 you assign it to an uncreated variable. If this still doesn't work please ask me :)

Joseph Frazer
Joseph Frazer
5,404 Points

Also, if your trying to set the string color equal to the parameter, do this:

class GoKart {
  private String color = "red";

  public Gokart(String color){
    public this.color = color;

  }

  public String getColor() {
    return color;
  }

}
andren
andren
28,558 Points

Probably just a bit of a typo on your end but variables inside methods / constructors does not use access modifiers (public, private, etc) that will cause the code to crash. They are only used for field variables, which are outside methods and belong to the class.

This is probably what you meant:

public Gokart(String color){
    this.color = color;
}
Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Check the case between your constructor and the name of the class. They need to be exactly the same.

Hope that helps!