this Keyword

AP Computer Science A· difficulty 3/5

public class A {
    public A() {
        System.out.print("hi ");
        this(5);
    }
    public A(int n) {}
}
// ...

What is the result?

  • A

    Nothing is printed

  • B

    Run-time error

  • C

    Compile-time error: this(...) must be the first statement

    check_circle
  • D

    hi printed

Explanation

A call to this(...) (or super(...)) must appear as the very first statement of a constructor.

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

Related questions