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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.