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

Lee Finch
PLUS
Lee Finch
Courses Plus Student 1,411 Points

i do not understand what i am doing wrong here. What do they mean by making public final int MAX_BARS = 8; static?

no details to add

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

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

  public String getColor() {
    return mColor;
  } 
}

1 Answer

Ruud Claassen
Ruud Claassen
9,359 Points

By making that value static, you can access the MAX_BARS variable without needing to create an instance of the GoKart class first. The MAX_BARS variable is now a class variable.

You can directly retrieve the value of MAX_BARS by using GoKart.MAX_BARS.

Further reading: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html