Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following class declaration: public class Thing { private int x; pr

ID: 3530636 • Letter: C

Question

Consider the following class declaration:

public class Thing
{
private int x;
private int y;
private static int z = 0;
public Thing()
{
x = z;
y = z;
}
static void putThing(int a)
{
z = a;
}
}

Assume a program containing the class declaration defines three Thing objects with the following statements:

Thing Thing();
Thing two = new Thing();
Thing three = new Thing();

a. How many separate instances of the x member exist?
b. How many separate instances of the y member exist?
c. How many separate instances of the z member exist?
d. What value will be stored in the x and y members of each object?
e. Write a statement that will call the putThing method.(5);

Explanation / Answer

z is a static variable, so only one instance of this field will ever exist. The number of instances of x and y depends on the number of instances of the Thing class.

z is initialized to 0, then the value in z is assigned to x and y in the constructor, so they contain the values 0 and 0.

To call the putThing method, you would just write something like:
putThing(16);

Since the method is static, you do not need an instance of the class to call that method.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote