this Keyword

AP Computer Science A· difficulty 2/5

public class Person {
    private String name;
    public Person(String name) {
        name = name;
    }
    public String getName() { return name; }
}
// ...
Person p = new Person("Bo");
System.out.println(p.getName());

What is printed?

  • A

    null

    check_circle
  • B

    Bo

  • C

    name

  • D

    Compile-time error

Explanation

Without this.name = name the assignment only sets the parameter to itself; the instance variable name remains its default null.

Want 10 more like this — adaptive to your weak spots?

Related questions