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

How do I make a new GoKart

I understand everything separately, but I don't know how to put it all together

Example.java
public class Example {

    public static void main(String[] args) {
        System.out.println("We are going to create a GoKart");
        GoKart color = new GoKart
        System.out.printf("The GoKart is %s\n", dispenser.getColor());

    }
}

1 Answer

Chase Marchione
Chase Marchione
155,055 Points

1) Your current code names the new GoKart color, rather than uses a color as a parameter for the new GoKart.

We know that the GoKart class has been specified so that its objects need one parameter, which is color. What we're going to do is choose a color and put it in double quotes, so that it is a string (remember that the mColor variable from the previous challenge was a String.) I chose red in my example.

2) You will want to call the getColor() method on the new GoKart object. Since the GoKart object (I've named mine newKart) is a new instance of GoKart, it has a getColor() method.

        GoKart newKart = new GoKart("red");
        System.out.printf("The GoKart is %s\n", newKart.getColor());

Hopefully my explanations have made the idea of constructors and how they can be called to create/instantiate objects a little clearer.