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 trialMayamiko Nanthambwe
1,365 PointsNow let's add a private uninitialized field to store the current number of energy bars. Name it mBarsCount.
private int mBarsCount; mBarsCount = 0;
what am i doing wrong?
public class GoKart {
public static final int MAX_BARS = 8;
private String mColor;
private int mBarsCount;
public GoKart(String color) {
mColor = color;
}
mBarsCount = 0;
public String getColor() {
return mColor;
}
}
3 Answers
mrben
8,068 PointsYour declaration of mBarsCount
is correct. You need to initialise mBarsCount
(i.e. set it to 0 in this case) in the constructor though, currently it's outside it.
Raymond Wach
7,961 Pointsmrben is correct. You should move the initialization statement for mBarsCount
inside the constructor for GoKart
. While it is also valid Java to provide a default initialization with the declaration, that would not be an "uninitialized field". Your code as is, includes an unreachable statement. Just move the line initializing mBarsCount
so that it is between the curly braces of the GoKart
constructor.
Mayamiko Nanthambwe
1,365 PointsThank you!!