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

I cant figure out how to create a new GoKart object

Hi,

I'm on task 1 of 2 of the last challenge for Java Objects. It says I need to create a new GoKart object using the parameter of color. I'm not sure how to go about this as I have tried everything. Thanks for the help. If you could include some code, that would be really helpful. Thanks!

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("Red");
      System.out.printf("The GoKart Color is %s\n", GoKart.getColor());

    }
}

1 Answer

David Lacedonia
David Lacedonia
13,627 Points

Is not GoKart.getColor(), is color.getColor()

because the object named 'color' has that method, no the type of object

Christopher Augg
Christopher Augg
21,223 Points

David is correct. The issue is your attempt to call GoKart.getColor() instead of color.getColor(). Thanks David!

I would also like to point out that it is always a good practice to provide relevant names for our variables. For example, when we create an object of type GoKart and assign it to a variable named color, it could very easily become confusing to read later by you or other developers. This is because one would most likely think color holds a reference to a Color object and not a GoKart object. Therefore, it would be better to name your variable with something like:

       GoKart marioKart = new GoKart("Red");

The code is creating a new GoKart object that is red in color and assigning its reference to marioKart. Therefore, marioKart is now a red goKart. It would then make more sense when calling the getColor() method of your newly created marioKart:

      System.out.printf("The GoKart Color is %s\n", marioKart.getColor());

Regards,

Chris