Output of a constructor with a shadowed field

Consider the following Java class, written for a bookstore inventory program.


Java
public class Book {
    private String title;
    private double price;

    public Book(String title, double price) {
        title = title;
        this.price = price;
    }

    public String getTitle() { return title; }
    public double getPrice() { return price; }
}


What is printed when the following code runs?


Java
Book b = new Book("Dune", 18.99);
System.out.println(b.getTitle() + " " + b.getPrice());
Choose an answer

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: