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 Meet Objects Add a Constructor

harun west
harun west
1,732 Points

Finally, since the color is being set in the constructor now, remove the initialization from the field definition...

Finally, since the color is being set in the constructor now, remove the initialization from the field definition. Just leave it declared, but not initialized to "red".

I have absolutely no idea what this means. What is meant by "field", "initialization" and "declared"?

GoKart.java
class GoKart {
  private String color = "red";

  //constructor
  public GoKart(String color){
    this.color = "red";
  }

  public String getColor(){
    return color;
  }

}

2 Answers

Fahad Mutair
Fahad Mutair
10,359 Points

hi harun west ,

remove the initialization from the field definition

which means

private String color;
harun west
harun west
1,732 Points

Thank you for the answer Fahad. I changed private String color = "red"; to private String color;

but it still says color is still being assigned in the field declaration...?

Fahad Mutair
Fahad Mutair
10,359 Points
class GoKart {
  private String color;

  //constructor
  public GoKart(String color){
    this.color = color;
  }

  public String getColor(){
    return color;
  }

}
cherishli
cherishli
654 Points

Hey for anyone looking for help in 2020, here it goes. Just developing on Fahad's point:

You need to change the initial answer of: private String color = "red";

In the second line to -

private String color = "color";

Hope that helps.