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 Harnessing the Power of Objects Add a Constant

Make sure that you've marked the MAX_BARS field with the static keyword. Which static keyword?

I can't seem to get this right. Asking me to have a static keyword, but I'm a bit confused to what and where I should put. Where am I going wrong?

GoKart.java
class GoKart {
  public final int MAX_BARS = 8;
  private String color;

  public GoKart(String color) {
    this.color = color;
  }

  public String getColor() {
    return color;
  }

}

2 Answers

andren
andren
28,558 Points

They are talking about the keyword literally called static. The static keyword is placed before a declaration like you do with keywords like public or final.

So the task wants the MAX_BARS declaration to look like this:

public static final int MAX_BARS = 8;

The static keyword makes it so you can access something directly from a class, without having to make an instance (object) from the class.

In other words without the static keyword you would need code like this to access MAX_BARS:

GoKart goKartObject = new GoKart("Blue");
goKartObject.MAX_BARS;

With the static keyword you can access MAX_BARS like this:

GoKart.MAX_BARS;

OOohhh... So simple! Thanks Andren!