Skip to main content

Lesson 0.1: Classes, and Objects

An object is an instance of a class, meaning that it is a specific thing that is made from the class.

To help you understand, think about a house blueprint. The blueprint states that each house has a street address, a paint color, and a front door. Builders can use the same blueprint more than once. The finished houses share a design, but each house has its own address, color, and door state.

Similarly, in Java:

House exampleJava meaning
The blueprintA class definition
One completed houseAn object created from the class
The house's address and paint colorField values stored by that object
Opening one house's doorCalling a method on one object

Example: Robot Arm

RobotArm is a class. It states what every arm object stores and what every arm object can do.

RobotArm example
class RobotArm {
private double power;

public void move(double requestedPower) {
power = requestedPower;
}
}

RobotArm frontArm = new RobotArm();
RobotArm rearArm = new RobotArm();

Both languages create independent RobotArm objects from the class definition. The syntax differs, but the blueprint-and-house relationship stays the same.

Java Keyword Explanations

Java reserves certain words for the language itself. These keywords have fixed meanings, so you cannot use them as the name of a class, field, or method.

WordWhat it tells Java
classStart a class definition. The name after it, such as RobotArm, names the class.
publicAllow code outside this class to use the class, constructor, field, or method.
privateAllow only code inside this class to use the field or method directly.
doubleStore a number that can have a decimal part, such as 0.75.
voidThis method performs work but does not send a value back to its caller.
newCreate an object, then run its constructor to prepare it.
thisRefer to the current object whose constructor or method is running.
returnEnd a method and, when required, send a value back to its caller.

Some important words in Java code are not keywords. String is a class provided by Java for text. RobotArm, power, and move are identifiers, which are names chosen by the programmer. A type appears before a field, parameter, or method name and states what kind of value it uses. For example, double power declares a field named power that stores a decimal number.

You do not need to memorize this table before continuing. Use it to translate each line into plain language.

public class RobotArm {
private String name;
private double power;

public RobotArm(String name) {
this.name = name;
this.power = 0.0;
}

public void move(double requestedPower) {
power = requestedPower;
}

public void stop() {
power = 0.0;
}
}

This code essentially makes the blueprint. It doesn't create a real arm yet.

RobotArm frontArm = new RobotArm("front");
RobotArm rearArm = new RobotArm("rear");

These declarations create separate objects from the RobotArm class. Each arm stores its own name and power.

frontArm.move(0.75);
rearArm.move(-0.40);

frontArm.move(0.75) changes only the front arm, while rearArm.move(-0.40) changes only the rear arm. Sharing a class does not mean sharing every stored value.

How Classes Can Help Your Team

A robot can use one arm in TeleOp and the same arm rules in autonomous. If those rules are defined in a class, both OpModes can call the same methods. One fix or addition can then automatically be implemented without needing other changes.

Terms to Keep

TermMeaning
ClassA definition that describes fields and methods.
ObjectA specific value created from a class while the program runs.
InstanceAnother name for an object created from a class.
StateThe field values currently stored by an object.
MethodA named operation defined by a class.

Check Your Understanding

RobotArm left = new RobotArm("left");
RobotArm right = new RobotArm("right");
left.move(0.5);
How many RobotArm objects exist?

Two. Every new RobotArm(...) expression creates a separate object.

Does right's power also become 0.5?

No. left.move(0.5) changes the object referred to by left. The other object keeps its own power value.

Which part is the blueprint?

The RobotArm class definition is the blueprint. The values created with new are the objects.

Where You Will Use This

Unit 13 turns this basic model into complete intake, lift, robot hardware, TeleOp, and autonomous project files.

Ready to move on?

Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.

Stuck on this lesson?

Ask about anything on this page. It can see which lesson you have open and which part you are reading.