3.3 Anatomy of a classInside a class: private data, public methods

A class starts with its header, public class StepCounter, and everything else lives between its braces. Inside, by convention, come the instance variables first, then the constructors, then the methods. Java doesn't care about that order, but readers do, and so does anyone grading your FRQ.
1public class StepCounter12{3 private String owner;24 private int steps;25 private int goal;267 public StepCounter(String owner, int goal)38 {9 this.owner = owner;10 this.goal = goal;11 steps = 0;12 }1314 public void walk(int n)415 {16 steps += n;17 }1819 public int stepsLeft()520 {21 if (reachedGoal())22 {23 return 0;24 }25 return goal - steps;26 }2728 private boolean reachedGoal()629 {30 return steps >= goal;31 }32}
- line 1 Class header: public class, then the name
- line 3-5 Instance variables: private, one set per object
- line 7 Constructor: same name as the class, no return type
- line 14 void method: changes the object, gives nothing back
- line 19 Non-void method: returns an int
- line 28 private helper: only code inside the class can call it
?Wait, what do public and private actually control?
Here's the rule for this course. The class itself is public. Instance variables are always private. Constructors are public. Methods are public when clients need them and private when they are only helpers for the class's own code, like reachedGoal. This is encapsulation: the data is sealed inside the object, and the only way in is through methods the author chose to offer.
Why seal it? Because then the author controls every change. If steps were public, any client could write mine.steps = -500 and the object would be nonsense. With steps private, the only ways to change it are the ones the class allows, so the class can keep its data valid.
1StepCounter mine = new StepCounter("Kai", 8000);2mine.walk(3500);13mine.walk(2700);14System.out.println(mine.stepsLeft());25mine.walk(2000);6System.out.println(mine.stepsLeft());3
- line 2-3 3500 + 2700 = 6200 steps so far
- line 4 8000 - 6200 = 1800 left
- line 6 8200 >= 8000, so the helper says the goal is reached
1// in a client class, not inside StepCounter2StepCounter mine = new StepCounter("Kai", 8000);3mine.steps = 99999;14mine.reachedGoal();2
- line 3 error: steps has private access in StepCounter
- line 4 error: reachedGoal() has private access in StepCounter
I thought about it. Show me
?Wait, what does steps += n do?
Notice that methods inside the class use steps and goal freely, with no dot and no getter. Instance variables belong to the object, and an instance method always runs on some object, the one before the dot in the call. When the client writes mine.walk(3500), the steps inside walk means mine's steps.
What does this client code print? Use the StepCounter class above.
StepCounter s = new StepCounter("Rui", 5000);
s.walk(1200);
s.walk(4100);
System.out.println(s.stepsLeft());
s.walk(-400);
System.out.println(s.stepsLeft());- The constructor sets owner to "Rui", goal to 5000 and steps to 0.
- walk(1200) makes steps 1200. walk(4100) makes it 5300.
- stepsLeft calls the helper: 5300 >= 5000 is true, so it returns 0. The first line printed is 0.
Why?
The helper runs on the same object, so it sees the same steps. - walk(-400) is legal: nothing stops a negative argument. steps becomes 4900.
Why?
This is a design gap. A comment could state the precondition n > 0, or the method could check it. You'll see preconditions in 3.5. - stepsLeft again: 4900 >= 5000 is false, so it returns 5000 - 4900 = 100.
Answer: It prints 0, then 100.
Stuck? Get a hint
- Write steps in a box and update it after every call.
- After the first two walks, steps is 5300. Is that at least the goal?
- walk(-400) subtracts. Then compare 4900 with 5000.
StepCounter s = new StepCounter("Lu", 5000);- As.walk(200);
- Bs.steps += 200;
- Cs.reachedGoal();
- DStepCounter.walk(200);
Show the answer
A. walk is public, so a client can call it on an object. steps and reachedGoal are private, so the second and third choices don't compile. The last choice calls walk on the class name, which only works for static methods, and walk is not static.

On the Class Design FRQ, one point is just for declaring the instance variables private. It's the easiest point on the exam. Never skip it, and never make an instance variable public to save time.
- Skeleton: public class header, private instance variables, public constructors, then methods.
- Encapsulation: data is private and reachable only through the methods the author offers.
- Clients use public methods; private helper methods serve the class's own code.
- Inside an instance method, an instance variable means the variable of the object the method was called on.