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 trialTony Reaves
10,422 PointsJava objects: "Remove the initialization from the field definition." What am I doing wrong?
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".
Unfortunately, every time I try to remove the color, or just set it to "color," I break the earlier tasks. Am I actually supposed to remove something from the code, or am I misunderstanding the question?
class GoKart {
private String color = "red";
public GoKart(String color){
this.color="red";}
public String getColor() {
return color;
}
}
1 Answer
Yanuar Prakoso
15,196 PointsHi Tony...
You need to declare the this.color = color which means you declaring that this color inside the class GoKart is defined by the color argument passed to the constructor method. Here is how you should revise your code:
class GoKart {
//private String color = "red";<-- you need to delete the "red" value and let it be just a String variable declaration like this:
private String color; //<-- this is what the task wants you to do
public GoKart(String color){
//this.color="red";<-- this declaration is wrong. You need to let the color argument passed to the constructor passed to this.color variable, like this:
this.color = color; //<- this is the correct way to declare constructor variable
}
public String getColor() {
return color;
}
}
I hope this can help a little