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 Incrementing

Android Development Stage 2: Harnessing the Power of Objects Challenge Task 1 of 1 Does syntax matter? (Public methods)

'''javascript

public void charge() { while (!isFullyCharged()) { mBarsCount++; } }

public boolean isBatteryEmpty() { return mBarsCount == 0; }

public boolean isFullyCharged() { return mBarsCount == MAX_ENERGY_BARS; }

'''

as you see, to complete the challenge I used isFullyCharged within a preceding method. In real code wouldn't this return a syntax error? Or does the fact that it's public mean it doesn't matter? Does java read code line by line top to bottom?

GoKart.java
public class GoKart {
  public static final int MAX_ENERGY_BARS = 8;
  private String mColor;
  private int mBarsCount;

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

  public String getColor() {
    return mColor;
  }

  public void charge() {
     while (!isFullyCharged()) {
    mBarsCount++;
     }
  }

  public boolean isBatteryEmpty() {
    return mBarsCount == 0;
  }

  public boolean isFullyCharged() {
    return mBarsCount == MAX_ENERGY_BARS;
  }


}

1 Answer

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

In real code it would not return an error. Java does read top to bottom, left to right, but it also needs to understand whatever is going on in your code. When Java gets the the point where it sees isFullyCharged it will realize that it has no clue what that means. So it goes in search for it. Just a you when you wake up and realize that you forgot your phone, you check the immediate area(The same method[your bed]) and then you increase your search from there(the same block of code[around your bed], the class[your room], and the package[your house]). It just does this a lot faster than you could search for your phone. I hope this helped. Happy Coding :)

ahh very cool thank you!