Java Objects
Objects
An object is one instance of a class. Use new and a constructor to create it. Each object has its own field values but uses the same methods defined on the class.
Creating objects
// Creating objects from a class
FRCRobot robot1 = new FRCRobot("CompetitionBot", 12345);
FRCRobot robot2 = new FRCRobot("PracticeBot", 67890);
FRCRobot robot3 = new FRCRobot("BackupBot", 11111);
// Each object has its own state
robot1.setBatteryLevel(95.0);
robot2.setBatteryLevel(60.0);
robot3.setBatteryLevel(100.0);
// All objects share the same methods
robot1.activate();
robot2.activate();
robot3.runDiagnostics();
System.out.println(robot1); // Uses toString method
System.out.println(robot2);
System.out.println(robot3);Object State and Behavior
State lives in instance variables; behavior is the shared methods on the class.
Multiple objects
public class RobotDemo {
public static void main(String[] args) {
// Create three different robot objects
FRCRobot speedBot = new FRCRobot("SpeedBot", 1001);
FRCRobot strongBot = new FRCRobot("StrongBot", 1002);
FRCRobot precisionBot = new FRCRobot("PrecisionBot", 1003);
// Each robot can have different configurations
speedBot.setMaxSpeed(100);
speedBot.setBatteryLevel(85);
strongBot.setMaxSpeed(60);
strongBot.setBatteryLevel(95);
strongBot.setLiftCapacity(50);
precisionBot.setMaxSpeed(40);
precisionBot.setBatteryLevel(90);
precisionBot.enablePrecisionMode(true);
// All robots can perform the same actions
speedBot.activate();
strongBot.activate();
precisionBot.activate();
// But their behavior might differ based on their state
speedBot.moveForward(10); // Moves fast
strongBot.liftObject(); // Uses high lift capacity
precisionBot.moveToCoordinate(15.5, 22.3); // Precise movement
// Display each robot's current state
System.out.println(speedBot);
System.out.println(strongBot);
System.out.println(precisionBot);
}
}Understanding Object References
Object variables hold references (addresses), not the object itself. Two variables can refer to the same object; updates through either name show up on both.
References example
// Create one robot object
FRCRobot mainRobot = new FRCRobot("TeamBot", 12345);
// Create another reference to the SAME robot object
FRCRobot robotAlias = mainRobot;
// Both variables point to the same object
mainRobot.setBatteryLevel(75);
System.out.println(robotAlias.getBatteryLevel()); // Prints 75!
// Changes through either reference affect the same object
robotAlias.setMaxSpeed(80);
System.out.println(mainRobot.getMaxSpeed()); // Prints 80!
// Comparing references vs. creating new objects
FRCRobot robot1 = new FRCRobot("Bot1", 1111);
FRCRobot robot2 = new FRCRobot("Bot2", 2222);
FRCRobot robot3 = robot1; // Same reference as robot1
System.out.println(robot1 == robot2); // false (different objects)
System.out.println(robot1 == robot3); // true (same object)
System.out.println(robot2 == robot3); // false (different objects)
// null reference - doesn't point to any object
FRCRobot nullRobot = null;
if (nullRobot != null) {
nullRobot.activate(); // This would cause NullPointerException!
} else {
System.out.println("Robot reference is null");
}Reference rules
- Variables hold references, not embedded objects
- Several variables can point at one object
nullmeans no object==compares references;.equals()compares content (when implemented)- Changes through any reference affect the same object
Objects as parameters
Methods receive a copy of the reference. The method can change the object’s fields; those changes remain visible after the call.
Parameters example
public class RobotManager {
// Method that takes robot objects as parameters
public void initializeRobot(FRCRobot robot, String configuration) {
System.out.println("Initializing " + robot.getName() + " with " + configuration);
robot.setBatteryLevel(100); // Modify the robot object
robot.activate();
if (configuration.equals("competition")) {
robot.setMaxSpeed(90);
robot.enableCompetitionMode(true);
} else if (configuration.equals("practice")) {
robot.setMaxSpeed(60);
robot.enableSafetyLimits(true);
}
System.out.println("Robot initialization complete");
}
// Method that compares two robot objects
public void compareRobots(FRCRobot robot1, FRCRobot robot2) {
System.out.println("Comparing robots:");
System.out.println(robot1.getName() + " vs " + robot2.getName());
if (robot1.getBatteryLevel() > robot2.getBatteryLevel()) {
System.out.println(robot1.getName() + " has higher battery");
} else if (robot1.getBatteryLevel() < robot2.getBatteryLevel()) {
System.out.println(robot2.getName() + " has higher battery");
} else {
System.out.println("Both robots have equal battery levels");
}
}
// Method that works with an array of robot objects
public void manageRobotTeam(FRCRobot[] robotTeam) {
System.out.println("Managing team of " + robotTeam.length + " robots:");
for (int i = 0; i < robotTeam.length; i++) {
FRCRobot robot = robotTeam[i];
if (robot != null) {
if (robot.getBatteryLevel() < 30) {
System.out.println("⚠️ " + robot.getName() + " needs charging!");
robot.returnToBase();
} else if (!robot.isActive()) {
System.out.println("▶️ Activating " + robot.getName());
robot.activate();
} else {
System.out.println("✅ " + robot.getName() + " is ready");
}
} else {
System.out.println("❌ Robot slot " + i + " is empty (null)");
}
}
}
}