When i make 2 object for a same class, and then i make one object equal to another, then changes made to one object affect another object also, then when i assign one object as "null" then why the change is not made to another object as well?
Here is my code for BoxMain.java
package object_assignment;
public class BoxMain
{
public static void main(String[] args)
{
Box yellowbox=new Box();
Box redbox=new Box();
yellowbox.height=12;
yellowbox.width=11;
yellowbox.length=22;
redbox=yellowbox;
System.out.println("volume yellowbox="+(yellowbox.height*yellowbox.width*yellowbox.length));
System.out.println("volume redbox="+(redbox.height*redbox.width*redbox.length));
redbox.height=15;
redbox.width=11;
redbox.length=21;
System.out.println("volume yellowbox="+(yellowbox.height*yellowbox.width*yellowbox.length));
System.out.println("volume redbox="+(redbox.height*redbox.width*redbox.length));
yellowbox.height=1;
yellowbox.width=11;
yellowbox.length=2;
System.out.println("volume yellowbox="+(yellowbox.height*yellowbox.width*yellowbox.length));
System.out.println("volume redbox="+(redbox.height*redbox.width*redbox.length));
redbox=null;
System.out.println("volume yellowbox="+(yellowbox.height*yellowbox.width*yellowbox.length));
System.out.println("volume redbox="+(redbox.height*redbox.width*redbox.length));
}
Here is Box.java
package object_assignment;
public class Box
{
int height;
int width;
int length;
}
So when this is executed it runs suucessfully but later causes an nullpointerexception.Now i know exception handling, but my doubt is that why the assignment of null to one aobject is not reflected in other object?