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

can't understand constructor in java.HELP ME

i am learning java but i am stuck on java objects.In the exercise i have to build a constructor for a GO KART but didn't understand it. SOMEONE EXPLAIN IT TO ME PLEASE

BTW you can press "Get help" in your challange (its a button) this way you challange gets linked the your forum post. This is useful so that we can look at your challange

1 Answer

I think this is the code of the challange right?

public class GoKart {
// we will work with this string...we will change and return its value
  private String mColor = "red";

/*****************************************************************
*So if someone writes GoKart("green"); the String color is "green"
* which means that mColor gets that value "green"
*****************************************************************/

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


/*****************************************************************
*The GoKart(String color) constructor changed the value of mColor so
*we return it now with the 'getter' getColor()
*****************************************************************/
  public String getColor() {
    return mColor;
  }
}

With the contructor here you "create or change" something while the getter returns the value... In practice we might would already return the mColor value in the GoKart method but w/e