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

Matt Nickele
Matt Nickele
468 Points

No Idea why this does not run

public class Example {

private String mKartColor;

public GoKart (String kartColor){ mKartColor = kartColor; }

public String getkartColor(){ return mKartColor; }

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

}

Example.java
public class Example {


  private String mKartColor;

  public GoKart (String kartColor){
    mKartColor = kartColor;
  }

  public String getkartColor(){
    return mKartColor;
  }

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

}

1 Answer

The challange already provide the class GoKart...it is already created by the challange (also i think you had to create it in the earlier challanges). It would be annoying to write everything agian... Most code you wrote is already part of the class GoKart. With GoKart SomeName = new GoKart("Color") we create a new instance of that class... which provides us the method getColor();

This simple code will pass the challange

public class Example {

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