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

help

Each battery has a maximum of 8 bars.

For this task, let's add a constant field to the class that stores the maximum number of energy bars. Make sure the field cannot be changed and is accessible from the class, not just the instance. Use the naming convention we learned.

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hey Marco,

For this, we have to create a static variable that cannot be changed. We want this variable to be accessible directly from the class. Do you remember how to do this?

public static final int MAX_BARS=8;

We write public because we want to access it from outside the code. We write static so we can access it directly from the class meaning we dont have to even create an object of the class to use it. We write final to make it unchangeable. And then lastly, we write the data type (we write int because we are storing a number) and then we make our variable all upper case as that is the naming convention with static variables.

I hope that made sense to you.

Happy coding and best of luck,

Kevin