This theory doesn't prevent you from modifying an object's state. Objects you pass to a method are not immutable. The reference to the object will not change, but it's content can be changed.
Check what happens with the following :
The reference to the object will not change, but it's content can be changed.
as you told reference will not change means inside the method. method reference and actual reference are pointing to the same object wright and after the method scope the method reference will be no more valid is it correct.l
santhosh.R gowda wrote:after the method scope the method reference will be no more valid is it correct.l
Yes this is correct. However the original reference will still be valid - you can still use it.
If there still confusion I'll recommend you get your hands on Head First Java. The got a really nice 'remote control' which should makes things crystal clear & a whole lot of other concepts too (Of course!)
1.class test 2.only one instance of class test is created.
test p = new test(100);
3.p is the reference variable which holds the address of object test, with i = 100.
4.in getRef(p) - you are passing the address of object test, as p holds the address. 5. public static void getRef(test obj)
{
obj.i=200;
}
inside this method ,
no new object is created.
obj has the same address as p.so obj and p refers the same object of class test, where the i's value is 100.
6. when you assign the value for i using this statement,
obj.i=200;
you are manipulating the one and only object, by changing its i value to 200.
7.that's how you get 200 as output,using this print statement.
System.out.println(p.i);