/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corejava;
/**
*
* @author Administrator
*/
public class CoreJava {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Test test=new Test();
test.setx(20);
test.printx();
Test test1=test;
test1.setx(40);
test1.printx();
test.printx();
int a=20;
int b=a;
a=40;
System.out.println(a);
System.out.println(b);
}
}
Output:
run:
X is 20
X is 40
X is 40
40
20
Now here when i set the value of 'x' in 'a' and assigned the object 'a' to 'b', 'b' will point to the same value of object 'a'. so any changes to 'a' will also change the value of 'b';
But when i used simple variables why didn't it do the same as for objects. Why the objects stored in heap and variables in stack area.