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 Creating New Objects

Isam Al-Abbasi
Isam Al-Abbasi
2,058 Points

Help with task 2 please!!

Hello everyone, could you please help me to pass this task? my mind completely gone blank and I don't even seem to understand what I am supposed to do!!

Question is: Add a public constructor to the GoKart.java class that allows the parameter color to be passed in. In the constructor store the argument color in the private color field.

Example.java
public class Example {

    public static void main(String[] args) {
        System.out.println("We are going to create a GoKart");
    }
}

3 Answers

private Color color;  // this is the instance variable that stores the color
public GoKart(Color theColor){  // this is the constructor 
  color = theColor; // this sets the color for your instance variable
}

The code that I just wrote will go inside of your class, which I didn't include by the way.

Isam Al-Abbasi
Isam Al-Abbasi
2,058 Points

Thank you so much Jeremy for your help, but I still find it hard to get the grasp of all these new terms such as class, constructor, instantiate etc... I'm starting to think that this is not for me :( I am so eager to lear Java programming but it looks like it's a lot harder than I thought it would be.

Normally when you run a program it usually wants some sort of input from the user. The constructor helps facilitate that. With this GoKart program even if all it does is change the color of your go kart it expects the user to tell it what color to be. So with that being said when you instantiate an object you use it's constructor to do that. For instance, The GoKart class when instantiated in another class would look something like this:

GoKart goKart = new GoKart(red);

I'm the user and I wanted it to be red so I instantiated it with red. This GoKart class is an object and you can recreate it as much as you want using the same color or different colors. But anytime that you instantiate (which is necessary to utilize the class)you use the constructor whether it is the default (not user defined) or custom which is the one I typed above.