Methods: How to Write Them
Void vs. Non-Void, Revisited for Your Own Classes
Same rule as Ch.4, now applied to methods you write inside your own class: void methods act (no return value); non-void methods compute and return one.
public class Intake
{
private double currentSpeed;
public void spin(double speed) // void — acts, doesn't return anything
{
currentSpeed = speed;
}
}
Getters (Accessors)
Since instance variables are private (Lesson 7.1), other classes need a way to read them — a getter: a public method, no arguments, that returns one instance variable's value. By convention its name starts with get.
public double getCurrentSpeed()
{
return currentSpeed;
}
You don't need a getter for every instance variable — only the ones other code genuinely needs to read.
Setters (Mutators)
A setter is the write-side equivalent: a void method, named starting with set, taking one parameter — the new value — and assigning it to the instance variable.
public void setCurrentSpeed(double newSpeed)
{
currentSpeed = newSpeed;
}
Think just as hard before writing a setter as before writing a getter. Not every instance variable should be freely settable from outside — sometimes the class should only change its own data through a specific method (like spin(speed) above) that also enforces some rule, rather than through a bare setter that lets anyone assign anything.
toString
toString is a special method Java calls automatically whenever it needs to turn an object into text — including every time you System.out.println(someObject) or concatenate an object with +.
public String toString()
{
return "Intake[speed=" + currentSpeed + ", deployed=" + isDeployed + "]";
}
Without a toString, printing an object directly gives an unhelpful default (something like Intake@1a2b3c) — writing your own gives every System.out.println(myIntake) a genuinely useful message instead.
Method Parameters, Revisited
Same call-by-value rule from Ch.4.1: a primitive parameter (double speed) gets a copy. Changing it inside the method never affects the caller's original variable.
Common Pitfalls
- Writing a getter or setter for every instance variable reflexively. Only write one when something outside the class genuinely needs to read or change that specific value — a setter in particular can undermine whatever rules the class's own methods were meant to enforce.
- Forgetting
toStringand being surprised by ugly default output. If you're going to print an object for debugging or status,toStringis worth the two minutes it takes to write.
Key Takeaways
- Getters return an instance variable's value (no parameters); setters assign a new value (one parameter,
voidreturn). - Not every instance variable needs a getter or setter — only add one when something outside the class actually needs it.
toStringis called automatically whenever Java needs to convert an object to text — write your own for useful debug output.- Method parameters follow the same call-by-value rule as before: primitives are copied.
Derived from csawesome-2026: unit-3-class-creation/3.05-methods-how-to-write-them.md (CSA 3.5)