Repair a name inside its own initializer

A booking program should initialize a local seat count from the class's stored count, allowing for six unavailable seats. In this code, Hall.seats explicitly accesses the field declared in Hall.


Java
class Hall {
    static int seats = 90;

    static void prepare() {
        int seats = seats - 6;
        System.out.println(seats);
    }
}


Complete the diagnosis and repair. For the first gap, choose compilation or execution. For the second gap, choose local variable or class field. For the final gap, enter the field access using the class name, with no spaces.


The original program is rejected during because the right-hand seats in the local declaration refers to the before it has received a value. Keeping the local variable's name unchanged, replace only that right-hand seats with .

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 Variable Declarations and Scope in Java Questions: