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 Changing State

how can i set the barCount available to maximum amount of bar count?

sorry, any one can help me please !

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

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

  public String getColor() {
    return color;
  }
  public void fill (charge) {
private int barCount = MAX_BARS ;
  }
}

1 Answer

Eric M
Eric M
11,545 Points

Hi Allen,

The private int barCount should exist with the other member variables so that it is a property of the object rather than just the charge method.

Then in the charge method you can set the object's instance of barCount to the object's instance of MAX_BARS.

Hope that helps

sorry ecmk ,can you help me with screenshorts?

Eric M
Eric M
11,545 Points

Happy to elaborate, this stuff is fundamental but there's so much new terminology it can get confusing fast.

Everything we're doing is happening inside the GoKart class. First things first, we need to add a new member variable to the class called barCount, its type is int, and its private.

If we look at just the class and its member variables (i.e. if we remove the methods from the example), we have:

class GoKart 
{
        public static final int MAX_BARS = 8;
        private String color;
}

There are currently two variables in this class. MAX_BARS does not change, this is set as an unchanging value by the final keyword, this is the final value it can be, it cannot be changed. Its value is constant. It's an integer, it can be accessed from outside of the class (it's public). color is a String that is private to instances of this class. It's probably only going to be operated on by methods within the class. We need to add a new int to these, barCount which is private. So our code block becomes:

class GoKart 
{
        public static final int MAX_BARS = 8;
        private String color;
        private int barCount;
}

We now need a method to set barCount to the value of MAX_BARS, let's take a look at the existing methods within the class:

class GoKart 
{
        public static final int MAX_BARS = 8;
        private String color;
        private int barCount;

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

        public String getColor() 
        {
                return color;
        }
}

We have two public methods.

One is the constructor, GoKart, which takes a string for the color of the GoKart and returns an instance of the GoKart class. (This return is implicit based on this method being a constructor, but don't worry about that for now. Just know that the constructor is named the same as the class, doesn't need a return type, and (usually) sets member variables for the instance it's constructing)

The second is getColor. getColor doesn't take any arguments; its brackets are empty. Its return type is String. All this method does is return the color of the instance, whatever was passed to the constructor earlier when the instance was instantiated. This type of method is commonly referred to as a getter.

We need to add a new method called charge which sets barCount to MAX_BARS. We don't need to take in any arguments to do this (we already have all the information we need in the class) so we can leave our arg list empty. We don't need to return anything either but we do need to specify a return type in Java, so we use void. It's a public method too, so we'll end up with public void charge().

I'm sure you can get it from here, but since you asked for screenshots, here's how we put it together:

class GoKart 
{
        public static final int MAX_BARS = 8;
        private String color;
        private int barCount;

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

        public String getColor() 
        {
                return color;
        }

        public void charge()
        {
                barCount = MAX_BARS;    
        }
}