Constructors

AP Computer Science A· difficulty 2/5

public class Dog {
    private String name;
    public void Dog(String n) {
        name = n;
    }
}

What is wrong?

  • A

    Constructors cannot accept parameters

  • B

    Nothing is wrong

  • C

    Constructors have no return type, not even void

    check_circle
  • D

    name should be public

Explanation

Including void makes Dog a regular method, not a constructor; remove void so it has no return type.

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

Related questions