• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

object is assigned to null but not reflecting

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic