Shared references to a running total

A warehouse records scanned units using this Java function object. Each invocation adds its argument to the object's stored unit count and returns the updated count.


Java
class RunningUnits {
    private int units = 0;

    public int apply(int addedUnits) {
        units += addedUnits;
        return units;
    }
}


The following statements execute in the order shown:


Java
RunningUnits first = new RunningUnits();
RunningUnits second = first;
int initial = first.apply(4);
int middle = second.apply(9);
int last = first.apply(2);


Determine middle and last. Give both answers in units as exact integers.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Function Objects Questions: