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

Could someone help me out please?....

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

public String getColor() { return mColor;

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

Ive look at other peoples answers to compare and i still seem to be missing something.. any ideas?

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

  public String getColor() {
    return mColor;

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

4 Answers

Christopher Augg
Christopher Augg
21,223 Points

Hello Doug,

Very close man. Here is a hint:

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

        public String getColor() {
          return mColor;

      // Your error is here. You always need to close things you open. 

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

I hope this helps.

Regards,

Chris

Actually, I was putting the code in the wrong place...lol. Now im having trouble creating a new GoKart....ugh this stuff is so hard to retain...

Christopher Augg
Christopher Augg
21,223 Points

All you were missing in the code you provided was a }

The following works:

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

        public String getColor() {
          return mColor;

     }

       public GoKart(String color) {
        mColor = color;
  }
}
Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Doug,

your code should look like this:

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

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

For creating a new GoKart take this code:

GoKart newGoKart = new GoKart("green");

With this code you create a new object/instance of the GoKart. With the keyword "new" you instantiates your new object with the help of a constructor. The constructor takes the color "green" and gives it to your new GoKart object "newGoKart" The Instantiation means creating a new object/instance of a GoKart class. Thats why you make a constructor with the same name as the class. So in the line

new GoKart("green");

the GoKart ist the name of the constructor you are calling.

I hope my enghlish is well enough to explain this topic ...

Best wishes from germany

Yeah i got it finally lol...just need to quit tryin to learn this stuff at 4 in the morning lol..Thank you for the timely response and best wishes from USA and Happy Coding!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

LoL

I know this feeling ....

Keep on rockin` ...