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

Robert Roberson
PLUS
Robert Roberson
Courses Plus Student 8,401 Points

I have no idea what I am to do here.

I don't even know what to ask.

GoKart.java
public class GoKart {
  private String mColor;

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

  public String getColor() {
    return mColor;
  } 
}

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi Robert,

This is going to take a little while to explain as it's a 3 part challenge. I'll give you a head start but you'll find the answers you need in the previous video to this challenge.

Let's look at the first question.

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.

So what you want to do is simply define a new variable. But it's a constant value i.e. a value that will never change through the life of the program. Namely the maximum number of bars in the Kart is 8.

How you differentiate between a Constant and another variable is the access modifers and you put the name in CAPITALS. If you declare a variable as final, you're telling the compiler that the value cannot be changed. Adding the name in capitals is a naming convention letting us know that the value is a constant.

 public static final int MAX_PEZ = 12;

Add this to the top of your class. Good luck.

Robert Roberson
PLUS
Robert Roberson
Courses Plus Student 8,401 Points

Thanks, I have figured it out and am now trying to do the next step that is even murkier to me.