A local variable that shadows a field

A scorekeeping program uses the following Java class.


Java
public class Tally {
    private int total;

    public void add(int amount) {
        int total = this.total + amount;
    }

    public void addTwice(int amount) {
        total += amount;
        total += amount;
    }

    public int getTotal() {
        return total;
    }
}


The program runs the statements below. What value does the final statement print? Give your answer as an integer.


Java
Tally t = new Tally();
t.add(40);
t.addTwice(15);
t.add(25);
t.addTwice(10);
System.out.println(t.getTotal());

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 Instance Variables and Class Variables Questions: