Objects — Instances of Classes
Class as Blueprint, Object as Instance
A class is a blueprint; an object is one specific thing built from it — the way a cookie cutter (class) stamps out cookies (objects), all the same shape but each its own cookie. TalonFX is a class; leftMotor and rightMotor are two separate objects built from it.
TalonFX leftMotor;
TalonFX rightMotor;
A class also defines a new type, the same way int or double do — TalonFX leftMotor; declares a variable exactly like int score; does, just holding a TalonFX instead of a whole number.
Attributes and Behaviors, Revisited
Every object has:
- Attributes — the data it holds (its CAN ID, its current speed).
- Behaviors — what it can do, defined as methods (
set(),getVelocity()).
The class defines what attributes and behaviors exist; each individual object has its own values for them. leftMotor and rightMotor are both TalonFX objects, but each has its own CAN ID and its own current speed.
Reference Variables
A variable like leftMotor doesn't hold the motor object directly — it holds a reference to it, like a tracking number pointing at where the real object lives in memory. This is different from a primitive variable (int, double), which holds its value directly with nothing in between.
You can create as many objects from one class as you need:
TalonFX leftMotor = new TalonFX(1);
TalonFX rightMotor = new TalonFX(2);
TalonFX intakeMotor = new TalonFX(3);
Three separate objects, one class.
Common Pitfalls
- Confusing the class name with an object's name.
TalonFXis always the class (capitalized, PascalCase);leftMotoris a specific object (camelCase). Mixing these up is a common early error. - Assuming two objects of the same class share state.
leftMotorandrightMotorare bothTalonFXobjects, but changing one's speed has zero effect on the other — each object's attributes are entirely its own.
Key Takeaways
- A class is a blueprint; an object is one specific instance built from it.
- A class defines a new type, just like
intordouble— a class variable declares a variable of that type. - Attributes are an object's data; behaviors are what it can do, defined as methods.
- A reference variable holds a pointer to an object, not the object's data directly.
- Many objects can be created from the same class, each with its own independent attribute values.
Derived from csawesome-2026: unit-1-using-objects-and-methods/1.12-objects-instances-of-classes.md (CSA 1.12)