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

What is wrong in this code

question is Let's ensure that all GoKarts created have a color. We'll solve this over a few steps.

First, define a public constructor that expects a String argument named color. (Don't worry about adding any code inside the constructor just yet, we'll do that next.)

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

  public String GoKarts(String color){

    }



}
Raul Faure
Raul Faure
596 Points

Hi Archie, remember that the constructor uses the same name as the class? This is only a hint and I hope it helps...please let me know.

3 Answers

Hi Archie, constructor declarations look like method declarations—except that they use the name of the class and have no return type. In your code the constructor has a return type String.

class GoKarts { private String color = "red";

public String GoKarts(String color){

}

}

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