Consider the following class declaration.

public class Thing

{

private String color;



public Thing()

{

color = "Blue";

}



public Thing(String setColor)

{

color = setColor;

}

}

Which of the following code segments, when appearing in a class other than Thing, would create a reference of type Thing with a value of null ?


Thing someThing = new Thing("Green");

A

Thing someThing = new Thing("");

B

Thing someThing = new Thing();

C

Thing someThing;

D

Thing("Green");
E

Respuesta :

Thing someThing; is the right answer

Explanation:

A. Thing someThing = new Thing("Green");

This would create a reference type with value "Green". This is because this calls the constructor with one parameter.

B. Thing someThing = new Thing("");

This would create a reference type with value "" (empty). This is because once again it calls constructor with one parameter.

C. Thing someThing = new Thing();

This would set an object with value as "blue". This is because in no argument constructor, the value of the color is set to blue.

D. Thing someThing;

This is the right answer, because it will declare an object but it will not create it. So it has only null value by default.

E. Thing("Green"); This is an invalid statement.

ACCESS MORE