This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object is assigned to null but not reflecting

 
sigamala viswanath
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Reddy,
I had update my profile, Thanq for the information.
Hope now my profile fits according to java ranch rules.

Thanks,
Sigamala
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic