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

Matthew Francis
Matthew Francis
6,967 Points

[Java] Newbie - In this situation, how would you create an object?

My Challenge task states to:

Now that you've created the GoKart class. Please create a new GoKart object using the GoKart class, or blueprint. As you know it takes a single parameter, color when being built.

My code attempt is right below, any ideas where I went wrong and how to fix them?

Example.java
public class Example {
    public static void main(String[] args) {
        System.out.println("We are going to create a GoKart");
    }
  nGoKart = new GoKart("red");
  console.printf("the color of the GOKart is %s", mColor);
}

//Object:
public class GoKart{//I think I need to declare this on another field called GOKart.java? but the challange task only rovided me an Example.java; I must be doing it wrong then..
  public String mColor;
  public Example(String Color){
    mColor = Color;
  }
}

1 Answer

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

You should not define the GoKart class, the GoKart class has been given to you for this challenge.

Creating a GoKart instance: The creation is correct, but it should happen inside the main method, and you haven't declared nGoKart yet, so you need to declare it by writing its type before the variable name:

GoKart nGoKart = new GoKart("red");

For task 2 there are a couple of problems with this code:

console.printf("the color of the GOKart is %s", mColor);

First of all, it should also go inside the main method. Second, you should use System.out.printf to print the color. Third, you do not have access to mColor, since this is a private field inside GoKart, so you need to use the getter method: getColor(). The line therefore should be:

System.out.printf("the color of the GOKart is %s", nGoKart.getColor());

Hope this helps