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 am stuck on the Constructors Challenge. Please help. I am unsure of what I am doing.

The error is on: public GoKart (String mColor); { Please help.

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

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

   public GoKart gokart = new GoKart("red"); 

   public String getColor() { return mColor; };
 }

2 Answers

Athanasios Kourtzis
Athanasios Kourtzis
6,437 Points

Just remove the semicolon after the closed parenthesis in the constructor.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Madison,

I have modified and commented your code ....

public class GoKart { 
   private String mColor = "red"; 
//this is perfect declared and initialized member variable mColor

public GoKart (String color) {
// no semicolon required
//the Name of the Parameter that expects the constructor shouldn´t be the same as the member variable

mColor = color;
// the mémber variable is equal to the Parameter passed to the constructor
}

//GoKart gokart = new GoKart("red"); 
        // in this challenge you don´t Need to create a new object
//you dont need to write public bevor creating an object

public String getColor() { 
return mColor;
 }

 }

I hope it helps and you can grab the logic. If not don´t hesitate to ask :)

Grigorij

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

  public GoKart(String mColor) {
 /* The name of the Parameter can actually be the same as the variable 
          as long as you use "this." and it's more efficient this way. */
     this.mColor = mColor;
  }

  public String getColor() {
    return mColor;
  }

}