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 trialWayne Greenwood
15,907 PointsNow totally confused with the Constructor Quiz i have used may different ways to complete this test. Could someone help.
my first code is
public class GoKart { private String mColor = "red"; public GoKart (String mColor){ mColor = Color; } public Gokart gokart = new GoKart("red"); public String getColor() { return mColor; } }
2 Answers
Nick Beukema
6,359 Pointspublic class GoKart {
private String mColor = "red";
public GoKart (String mColor){
mColor = Color;
}
public Gokart gokart = new GoKart("red");
public String getColor() { return mColor; };
}
So the issue is, your constructor needs to reference the correct variable.
public GoKart (String mColor){
this.mColor = mColor
}
Where
this.mColor
references the GoKart object's instance variable, mColor.
and
mColor
references the parameter you define and pass a variable through in this line of the constructor
public GoKart (String mColor){
I think that should do it for you.. I'm not 100% sure on your section here.
public Gokart gokart = new GoKart("red");
This might need to be declared outside of the class declaration, but I could be wrong.
Mukul Gupta
8,760 PointsWhen declaring the constructor, use a different variable name than the member variable
public GoKart (String color){
mColor = color;
}
Wayne Greenwood
15,907 PointsWayne Greenwood
15,907 PointsCheers Nick most appreciated thats done it. It now makes sense.