The Class Blueprint
The Three Anatomical Parts
Every class you write has the same three main parts, in this order:
public class Intake
{
// 1. instance variables — the data each object owns
// 2. constructors — initialize the instance variables
// 3. methods — the behaviors
}
- Instance variables hold the data — each object gets its own separate copy. Two
Intakeobjects can each have their owncurrentSpeed. - Constructors initialize those instance variables when an object is built with
new. - Methods define what the object can do, and can read/change its instance variables.
Designing a Class: What Data Does It Represent?
Before writing any code, ask: what does this thing need to know about itself? For an Intake subsystem: is it currently deployed? What's its current motor speed? Does it have a sensor reading a game piece?
public class Intake
{
private boolean isDeployed;
private double currentSpeed;
private boolean hasGamePiece;
}
Encapsulation: Why Instance Variables Are private
public and private control who can access something. public means any other class can reach it; private restricts access to code inside that same class only.
Instance variables should almost always be private — this is encapsulation: the class's internal data is hidden, and other code can only interact with it through the methods the class chooses to expose. This matters in practice: if currentSpeed were public, any other subsystem could set it directly, bypassing whatever safety logic the Intake class's own methods enforce (a ramp limit, a max-speed clamp). Keeping it private means the only way to change it is through Intake's own methods, which can enforce those rules every time.
public class Intake
{
private double currentSpeed; // only Intake's own code can touch this directly
}
Putting It Together
public class Intake
{
// 1. instance variables
private boolean isDeployed;
private double currentSpeed;
// 2. constructor
public Intake()
{
isDeployed = false;
currentSpeed = 0.0;
}
// 3. methods
public void deploy()
{
isDeployed = true;
}
}
Any class can also have a main method — usually not for the class's normal job, but as a quick way to test that one class in isolation.
Common Pitfalls
- Making instance variables
public"to keep things simple." This defeats encapsulation — any other class can then set them to anything, with none of the class's own safety logic enforced. - Skipping the design step. Jumping straight to code without first asking "what does this class need to know about itself" tends to produce classes with the wrong data, discovered only after writing several methods around it.
Key Takeaways
- Every class has the same three parts, in order: instance variables, constructors, methods.
- Instance variables are per-object — each object gets its own independent copies.
- Instance variables should be
private; methods that are meant to be used by other classes arepublic. This is encapsulation. - Design a class by first asking what data it needs to represent, before writing any code.
Derived from csawesome-2026: unit-3-class-creation/3.03-anatomy-of-a-java-class.md (CSA 3.3)