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 trial

Java Java Objects (Retired) Harnessing the Power of Objects Methods and Constants

Now let's add a private uninitialized field to store the current number of energy bars. Name it mBarsCount.

Now let's add a private uninitialized field to store the current number of energy bars. Name it mBarsCount. Initialize it to zero in the constructor.

I have been stuck on this challenge for a little while now, I have watched the video quite a few times but still have been unable to complete the challenge.

2 Answers

Justin Kraft
Justin Kraft
26,327 Points

Uninitialized means that the variable to define is not assigned a value. This can be seen in the code below. Instead, we assign a default value of 0 when an object of this class is instantiated.

public class GoKart { private String mColor; public static final int MAX_ENERGY_BARS = 8; private int mBarsCount;

public GoKart(String color) { mColor = color; mBarsCount = 0; }

public String getColor() { return mColor; } }

aakarshrestha
aakarshrestha
6,509 Points

This is how it should be:

public class GoKart { private String mColor; public final static int BATTERY_BAR = 8; //Constant variable private int mBarsCount = 0; //variable assigned value 0

public GoKart(String color) { mColor = color; }

public String getColor() { return mColor; }

public void charge() { mBarsCount = BATTERY_BAR; //assigning value of the constact variable } }

Hope it helps!

Happy coding!!!