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 more lost than a blind man in a forest! HELP!

I've watched the video but it's just not clicking. And what purpose do constructors even have?!

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

  public GoKart(String mColor){
    mColor = color
  }

  public String getColor() {
    return mColor;
  }
}

1 Answer

Michael Hess
Michael Hess
24,512 Points

Hi SGT Awesome,

There are two things that are really important to understand in an object oriented programming language like Java.

  1. Classes
  2. Objects

A class is a blueprint for an object. An object is a thing -- like a goKart, a book or a bicycle.

Constructors helps us build objects. I feel like I may confuse you if I go further, so at this point, this is probably all you really need to know.

You're really close! But, there is a semicolon missing after color. Also the constructor argument should be String color.

Please see the following code:

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

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

If you have any further questions feel free to ask! Hope this helps!

This is a link to Oracle's Java tutorials covering constructors: Providing constructors for your classes

This is a link to Oracle's Java tutorials covering classes Declaring Classes

Thanks again for saving my bacon! ^_^