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

Thomas Guarneiri
Thomas Guarneiri
351 Points

Where am i going wrong?

It is asking to add a constructor, and i thought that was what the middle part of my code was, but apparently i am wrong and "forgot to add the constructor." If anyone could explain this part to me i would greatly appreciate it. I have watched the video over and over again and still have gotten confused.

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

  public String NewColor(String newColor){
   newColor=mColor; 
    return newColor;
  }

  public String getColor() {
    return mColor;
  }
}

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

You are almost there:

public class GoKart {
  private String mColor = "red";//instance variable

  public GoKart (String newColor){//argument
   mColor = newColor; 
//constructor doesnt need a return statement
//name of the constructor must be the same as the class Name
//you assign the argument newColor  to your instance variable mColor  
}

  public String getColor() {
    return mColor;
  }
}
David Axelrod
David Axelrod
36,073 Points

Hey Thomas! Turns out you dont have to return the new color as you are just setting it for future use. Image if you had to set more than one variable? just setting is passes the challange!! hope this helps!!

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