Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Meet Objects Creating New Objects

Keith Myers
Keith Myers
720 Points

Print out using System.out.printf the color of the new object, using the getColor method.

Keeps saying I have an error but I cant figure it out?

Example.java
public class Example {

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

2 Answers

You want to get the color of the GoKart object you just created, not just any GoKart. The way to point to that specific GoKart is the variable "a", because that's the variable you assigned your GoKart object to, when you created it. So you want to write a.getColor() to get the color of that one GoKart. "GoKart" isn't an object, it's just the class, the template of all GoKarts. It doesn't have a color per se. Only individual objects of type GoKart have a color.

You're asking the computer to print the color of a GoKart. Which one? You're using "GoKart.getColor()". This only works if there can only be one color for all GoKart objects. But that's not the case, so the computer needs to know which GoKart you mean. You need to reference the object you created just one line above instead of referencing the class.

Keith Myers
Keith Myers
720 Points

I still cant figure it out I keep having one error.