Constructors

AP Computer Science A· difficulty 3/5

Given:

<code>public class Dog { private String name; public Dog(String n) { name = n; } } </code></pre> Which is valid object creation?

  • A

    Dog d = new Dog("Rex");

    check_circle
  • B

    Dog d = new Dog;

  • C

    Dog d = new Dog();

  • D

    Dog d = Dog("Rex");

Explanation

Class has only a 1-arg constructor; calling no-arg <code>new Dog()</code> would be a compile error.

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

Related questions