Java - What is the output: final reference parameter?

Question

What is the output of the following code?

class Car {
  public String model="default model";
  public int year = 0;
  public double price = 0.0;
}

public class Main {
  public static void main(String[] args) {
    Car myCar = new Car();
    myCar.model = "Civic LX";
    myCar.year = 1999;
    myCar.price = 16000.0;

    System.out.println("#1: model = " + myCar.model + ", year = " + myCar.year + ", price = " + myCar.price);

    Main.test(myCar);

    System.out.println("#4: model = " + myCar.model + ", year = " + myCar.year + ", price = " + myCar.price);
  }

  public static void test(final Car xyCar) {
    System.out.println("#2: model = " + xyCar.model + ", year = " + xyCar.year + ", price = " + xyCar.price);

    // Let's make xyCar refer to a new Car object
    xyCar = new Car();

    System.out.println("#3: model = " + xyCar.model + ", year = " + xyCar.year + ", price = " + xyCar.price);
  }
}


Click to view the answer

xyCar = new Car(); //The final local variable xyCar cannot be assigned. It must be blank and not using a compound assignment

Note

xyCar is passed by constant reference value because it is declared final

Related Quiz