| Author |
object is assigned to null but not reflecting
|
sigamala viswanath
Greenhorn
Joined: Mar 13, 2006
Posts: 26
|
|
class objTest { int i,j; public static void main(String args[]) { objTest h1= new objTest(); h1.i=2; h1.j=3; System.out.println(" before swaping value are "+ h1.i+" "+h1.j); h1.swap(h1); System.out.println(" afer swaping value are "+ h1.i+" "+h1.j); } void swap(objTest obj) { int temp; temp=obj.i; obj.i=obj.j; obj.j=temp; System.out.println("Befor null Swap Function value are "+ obj.i+" "+obj.j); obj=null; } } Above program will swap the i & j intergers through the obj object. Above swap method i am making obj as null then also final values are getting me as swapped values only . May i know the reason behind this program? Thanks, Sigamala
|
 |
Kj Reddy
Ranch Hand
Joined: Sep 20, 2003
Posts: 1697
|
|
Hi Sigamala, Welcome to JavaRanch. Your display name is invalid as per JavaRanch Naming policy, your display name should have first name and last name. Please change your display as per naming policy. The same answer to your previous query will holds good to this thread too: http://www.coderanch.com/t/379400/java/java/swapping-function The reference changes to obj inside the swap are just visible to only swap() method, once obj comes out of swap method it will retain its old reference. But if you make any changes to properties(i, j values) it will reflected during entire life cycle of obj. Hope you got what I am saying. [ March 20, 2006: Message edited by: KJ Reddy ]
|
 |
sigamala viswanath
Greenhorn
Joined: Mar 13, 2006
Posts: 26
|
|
Hi Reddy, I had update my profile, Thanq for the information. Hope now my profile fits according to java ranch rules. Thanks, Sigamala
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: object is assigned to null but not reflecting
|
|
|