Skip to main content

Lesson 0.3: References and Object Identity

A Java variable that holds an object does not contain the whole object. It contains a reference that lets Java find that object.

In the house analogy, a reference works like a street address. Writing an address on two cards does not create two houses. Both cards lead to the same house. Java object variables behave the same way.

One Object, Two References

Two names for one object
RobotArm firstName = new RobotArm("lift");
RobotArm secondName = firstName;

secondName.move(0.8);
System.out.println(firstName.getPower()); // 0.8

Java creates one object because new appears once. firstName and secondName both refer to it. A change made through either reference reaches the same object.

Two Separate Objects

RobotArm firstArm = new RobotArm("front");
RobotArm secondArm = new RobotArm("rear");

firstArm.move(0.8);

Here new appears twice, so Java creates two objects. Moving firstArm does not change secondArm.

Count object creation by counting the new expressions that run, not by counting variable names.

A Reference to No Object

RobotArm arm = null;
arm.move(0.8); // NullPointerException at run time

null means the variable does not currently refer to an object. Calling a method through that variable fails at run time.

null is a Java keyword. It does not create an empty RobotArm, and it is not the number zero. It means "no object reference."

FTC code often hits this error when an OpMode uses a mechanism or hardware field before init() assigns it. The declaration creates the variable. It does not create the object.

Follow the Objects

RobotArm a = new RobotArm("A");
RobotArm b = a;
RobotArm c = new RobotArm("C");

b.move(0.4);
c.move(-0.2);
a.stop();

Trace it in order:

  1. a refers to the first object.
  2. b receives the same reference.
  3. c refers to a second object.
  4. b.move(0.4) changes the first object.
  5. c.move(-0.2) changes the second object.
  6. a.stop() changes the first object back to zero.

Terms to Keep

TermMeaning
ReferenceA value that identifies an object.
IdentityWhich specific object a reference reaches.
AssignmentStore a value or reference in a variable.
nullA reference to no object.
Run-time errorA failure that occurs after the program starts.

Check Your Understanding

How many objects exist in the trace above?

Two. The code evaluates new RobotArm(...) twice.

Which variables refer to the same object?

a and b refer to the first object. c refers to the second.

What are the final power values?

The first object ends at 0.0. The second ends at -0.2.

Where You Will Use This

Lesson 13.5 stores subsystem references inside one robot object.

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.