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 Privacy and Methods

Add a public method, or getter, that can be used to get the color of the go kart. help i'm stuck

tried to add public getColor(){ return mColor()} but didn't work. Can anyone help please?

GoKart.java
public class GoKart {
  public String mColor = "red";
 GoKart goKart = new Gokart("red");
  public getColor(){

    return mColor();
  }
}

5 Answers

Jan Van Raemdonck
Jan Van Raemdonck
22,961 Points

You forgot to add a return type to the definition of your method.

so it should be:

public String getColor(){
    return mColor;
  }

Also, don't try to create an object inside itself. So get rid of:

GoKart goKart = new Gokart("red");

inside of your GoKart class

Tks bro, but i have tried public class GoKart { public String mColor = "red"; public String getColor() { return mColor() ; } } before and didn't work. Any idea what i am missing?

Jan Van Raemdonck
Jan Van Raemdonck
22,961 Points

ah, i made a mistake in my answer, it should be

return mColor;

without parentheses. Sorry about that.

I edited my original answer

Thanks heaps bro Jan Van Raemdonck. Still confuse why you don't need to put parentheses though but it works ahaha.

Jan Van Raemdonck
Jan Van Raemdonck
22,961 Points

You add parentheses when you are calling or defining a method. in this case we are just returning a variable, so no parentheses are needed.

Luis Santos
Luis Santos
3,566 Points

I've tried everything and I still get it wrong, HELP!!!

The return type is missing for the answer. Missing String in this case after declaring accessor type "public" public String getColor()

Thanks Bro Jan.