Q Consider the following class public class IdentifyMyParts
Q. Consider the following class:
public class IdentifyMyParts
{
private int x = 7;
private int y = 3;
}
a. What are the instance variables?
b. What is the output from the following code:
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println(\"a.y = \" + a.y);
System.out.println(\"b.y = \" + b.y);
System.out.println(\"a.x = \" + a.x);
System.out.println(\"b.x = \" + b.x);
I need the answer to be compied to Word not a picture.
Solution
a) The instance variable is y
b) The output of the following above code is
a.y = 5
b.y = 6
a.x = 2
b.x = 2
IdentifyMyParts.x = 2
Here in the program x is defined as a public static int in the class IdentifyMyParts, every reference to x will have the value that was last assigned because x is a static variable (and therefore a class variable) shared across all instances of the class. That is, there is only one x: when the value of x changes in any instance it affects the value of x for all instances of IdentifyMyParts.
