Please answer the following questions by looking at the code
Please answer the following questions by looking at the code below
 1. What is the connection between box1 and box4? What happens (memory wise) when both box1 and box 4 are declared null? (20 points)
 2. In the Test2 class, there are two variables named a. What is the difference between the two variables? What concept does the output of the last three print statements demonstrate? Explain. (20 points)
 3. In the Rectangle class, explain the implementation of the Rectangle(int l) constructor. (10 points)
 4. In the Rectangle class, explain the implementation of the Rectangle(Rectangle other) constructor. (10 points)
 Rectangle.java
 public class Rectangle {
 public int length;
 public int width;
 public Rectangle(int l, int w) {
 length = l;
 width = w;
 }
 public Rectangle(int l) {
 this(l, 10);
 }
 public Rectangle(Rectangle other) {
 this(other.length, other.width);
 }
 }
 Test2.java
 public class Test2
 {
 static int a;
 public static void main(String[] args)
 {
 Rectangle box1 = new Rectangle(10,10);
 Rectangle box2 = new Rectangle(box1);
 Rectangle box3 = new Rectangle(10);
 Rectangle box4 = box1;
 box1 = null;
 System.out.println(\"box(l,w) = (\" + box2.length + \",\" + box2.width + \")\");
 System.out.println(\"box(l,w) = (\" + box3.length + \",\" + box3.width + \")\");
 System.out.println(\"box(l,w) = (\" + box4.length + \",\" + box4.width + \")\");
 box2 = null;
 System.out.println(\"\ \");
 a = 0;
 System.out.println(\"a = \" + a);
 int a;
 a = 100;
 System.out.println(\"a = \" + a);
 System.out.println(\"a = \" + Test2.a);
 }
 }
Solution
3) Rectangle(int l)
Takes length l as parameter ,uses it to initialize the length again along with breadth with value of 10 using other parameterized constructor
4) this implementation takes other rectangle object as parameter and initializes current object length and breadth with the values in other object using the other parameterized constructor
1) in the main method firstly box1 is created with attribute values 10 and 10
Later box4 reference is assigned the reference of box1 which means that box4 points to the memory address of box1 and hence both point to same object in memory.
Now box1=null makes the content in the box1 object memory address as null.hence box4 is also null now
2) one a is normal variable
Other a is static variable which belongs to class.
1st print gives 0
2nd print gives 100
3rd print calls static variable and gives 0


