1.14 Calling instance methodsAsking an object to do something

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?
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());
- line 1-2 Two objects, built by two different constructors
- line 3-4 Each call changes only its own object
- line 5 No new: c is a second name for a's object
- line 6 So this changes the object a refers to
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.
?Wait, why didn't this happen with ints?
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.
I thought about it. Show me
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());
- 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. - 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.
- r = q copies the arrow, so r also points to Q. r.add(100) takes Q to 300.
- 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. - p.getMl() is 260 and q.getMl() is 260 (both P). r.getMl() is 300 (Q).
Answer: 260 260 300
Stuck? Get a hint
- Draw two boxes for the two objects and keep their ml values up to date.
- For each variable, draw an arrow to the object it points to. Which lines move an arrow?
- After q = p, which object does q.add(10) change?
Bottle a = new Bottle(500, 100);
- Aa.add(10);
- Bint n = a.getMl();
- Cint m = a.drink(5);
- 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.