AP Computer Science A · Unit 1 lecture · Part 9 of 11 · 8 min

1.14 Calling instance methodsAsking an object to do something

Mr. Thompkins
Now we use our objects. You'll ask a bottle how full it is and tell it to add water, and you'll watch for the most famous trap in this unit: two variables pointing at the same object.

An instance method belongs to each object, so you call it on a specific object with the dot operator: a.add(300). The method works with that object's data. Calling b.drink(100) changes b's bottle and leaves a's alone. Every method on an object follows the same rules as any method: match the parameters, and know the return type.

A void method like add does a job and returns nothing, so you call it as a statement on its own. A non-void method like getMl() returns a value, and you should do something with it: store it, print it, or use it in an expression. Calling a.getMl(); alone is legal, but the value is thrown away, which is almost always a mistake.

?Wait, what's a void method?
A void method returns nothing. Its job is to do something, like change an object or print. Because there's no value, a void call can't go inside an expression or on the right of an =. int x = a.add(5); doesn't compile.
1Bottle a = new Bottle(500);12Bottle b = new Bottle(750, 250);13a.add(300);24b.drink(100);25Bottle c = a;36c.add(50);47System.out.println(a.getMl() + " " + b.getMl());8System.out.println(c.getMl() + " " + a.fractionFull());
  1. line 1-2 Two objects, built by two different constructors
  2. line 3-4 Each call changes only its own object
  3. line 5 No new: c is a second name for a's object
  4. line 6 So this changes the object a refers to
output › 350 150 350 0.7
Calling instance methods on two Bottle objects, run under Java 17 with the Bottle class above. Line 5 makes an alias.

Aliases

Line 5 is the important one. Bottle c = a; doesn't create a new bottle. There's no new. It copies the reference in a into c, so a and c are now two names for the same object. Two references to the same object are called aliases. When c.add(50) runs, it changes that one object, so a.getMl() reports 350 too.

Bottle aBottle cnullBottle b2ml 350, capacity 500one object, two namesb2.getMl() throwsNullPointerException
After Bottle c = a; both arrows point to one object. A variable holding null has no arrow, so calling a method on it crashes.
?Wait, why didn't this happen with ints?
An int variable holds its value directly. int y = x; copies the number itself into y, so changing y later doesn't touch x. A reference variable holds an arrow, so copying it copies the arrow, and both arrows lead to the same object.

If a variable holds null, there is no object to ask, so calling a method on it throws a NullPointerException at run time. The code compiles fine, because the compiler doesn't know the variable will be null when that line runs.

Pause and think
Bottle b2 = null; System.out.println(b2.getMl()); Does this compile? What happens when it runs?
I thought about it. Show me
It compiles, because b2 has type Bottle and Bottle has a getMl method. When it runs, b2 is null, so there's no object to call getMl on, and Java throws a NullPointerException.
Trace the references

Using the Bottle class, what is printed?

Bottle p = new Bottle(1000, 400);
Bottle q = new Bottle(1000);
q.add(p.getMl() / 2);
p.drink(150);
Bottle r = q;
r.add(100);
q = p;
q.add(10);
System.out.println(p.getMl() + " " + q.getMl() + " " + r.getMl());
  1. Two news, so two objects. Call them P (starts at 400 ml) and Q (starts at 0). p points to P, q points to Q.
    Why?
    Naming the objects separately from the variables keeps the arrows straight.
  2. q.add(p.getMl() / 2): p.getMl() is 400, divided by 2 is 200, so Q goes to 200. Then p.drink(150) takes P to 250.
  3. r = q copies the arrow, so r also points to Q. r.add(100) takes Q to 300.
  4. q = p moves q's arrow to P. Now p and q both point to P, and only r points to Q. q.add(10) takes P to 260.
    Why?
    Reassigning a reference moves the arrow. It doesn't copy or change any object.
  5. p.getMl() is 260 and q.getMl() is 260 (both P). r.getMl() is 300 (Q).

Answer: 260 260 300

Stuck? Get a hint
  1. Draw two boxes for the two objects and keep their ml values up to date.
  2. For each variable, draw an arrow to the object it points to. Which lines move an arrow?
  3. After q = p, which object does q.add(10) change?
Check yourself
Which line will cause a compile error?
Bottle a = new Bottle(500, 100);
  1. Aa.add(10);
  2. Bint n = a.getMl();
  3. Cint m = a.drink(5);
  4. Ddouble f = a.fractionFull();
Show the answer

C. drink is void, so it returns no value, and nothing can be stored in m. The other three are fine: add is used as a statement, and getMl and fractionFull return an int and a double that match their variables.

  • Call instance methods on an object with a dot: a.add(300). They use and change that object's data.
  • void methods are statements. Non-void methods return a value: store it or use it.
  • Assigning one reference to another (c = a) makes aliases: two names, one object.
  • Calling a method on null throws a NullPointerException at run time.