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 Meet Objects Add a Constructor

I would like help or an explanation to this please

I am not sure what to do

GoKart.java
class GoKart {
  private String color = "red";

  public String getColor() {
    return color;
  }

}
Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Hi there,

I don't see any code added yet.

The first part of the challenge is asking you to add a Constructor. Maybe review the Video on Constructors, and then give the challenge an honest try. If you're still stuck, you can post back with the code you are trying and the Community will be able to assist.

Keep Coding! :) :dizzy:

1 Answer

michaelcodes
michaelcodes
5,604 Points

Hi there! If you are confused with the video on constructors I will try to give a brief and simple explanation. The idea is that you do not want to assign values directly to a variable when you create a class. Since we think of a class as a template, we only want the variables declared but not any values assigned, as we will do that on an individual basis when we create the objects from the class.

Say we had the code that looked like this:

class GoKart {
  private String color = "red";
}

Every time we create a GoKart object from this class the object is going to be assigned the value of red.. That is not so useful. Instead we use a constructor (this is not the only reason we use them), which has the following format:

class GoKart {
   private String color;

    public GoKart(/*parameters*/) {
    //code to execute to assign values (such as for color)
  }
}

I won't fill in the values so you can practice using it with the code challenge :) Hope this helps, if you have any questions after reviewing don't hesitate to ask! take care and happy coding