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

William Forbes
William Forbes
21,469 Points

Initialization to zero

Hello, For the attached code the prompt was to "add a private uninitialized field to the constructor, then initialize it to zero". For the field mBarsCount shown in the code would:

" private int mBarsCount; " be the same thing as " private int mBarsCount = 0; "

I thought an unitialized int would just be expressed as "private int mBarsCount; " In other words was the wording just confusing and could it have just said "add a private field and initialize it to zero", or is there a functional difference between intializing to zero vs not initializing the field?

THanks!!

1 Answer

I think you are missing the constructor part.

public class GoKart {
  public static final int MAX_BARS = 8;
  private int mBarsCount; // declares the field

  private String mColor;

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

  public String getColor() {
    return mColor;
  }

  public void getBarsCount() {
    mBarsCount = 0; // initializes the field
  }
}