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 (Retired) Meet Objects Constructors

I DON'T GET WHAT THEY ARE SAYING!

PLEASE HELP!

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

2 Answers

It will work if you don't alter any of the other code. Here's what the completed challenge looks like http://goo.gl/yf6pkz

Hope that helps.

Steve.

Hiya,

A constructor creates the class and handles any parameters you pass in to make the new object of the class. In this case, you pass in a String to represent the colour of the Kart.

A constructor usually takes the same name as the class itself; GoKart in this case.

That gives you:

  public GoKart (String color) {
   mColor = color; 
  }

So, you create a class by calling the constructor and passing in a String for the colour. The constructor creates a new GoKart and sets its mColor member variable to equal the colour you passed in.

I hope that makes sense.

Steve.

This doesn't work