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

On Task 2, I'm getting a message which says "Task 1 is no longer passing". Any idea as to how to get around that?

On Task 2, I'm getting a message which says "Task 1 is no longer passing". This happens when I fill in anything (anything presumably incorrect, that is ) in the constructor. Any idea as to how to get around that?

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

}
  public String getColor() {
    return color;
  }

}

1 Answer

Matthew Caloger
Matthew Caloger
12,903 Points

The task is asking you to set the "color" value to whatever is passed into the constructor. To do that, you'll put this.color = color; inside of the constructor. It's important to note that when accessing private fields in a class, you refer to them using this.<fieldname>.

Full code:

class GoKart {
  private String color = "red";

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

  public String getColor() {
    return color;
  }
}