• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

references and method parameters

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of trying to compile and run the following code?
(Select one correct answer)
-----------------------------------------------------------------------

-----------------------------------------------------------------------
The answer is 0 null null
Can someone explain how? I thought this.str should give 0?
Thanks
[Dan changed the titled to something more descriptive and added formatting to the code block.]
[ March 01, 2003: Message edited by: Dan Chisholm ]
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hum....
what is the output of attempting to compile the following:
int i=2;
i=i+0;
i=i;// special atention on this!!!
System.out.println(i);
it produces 2
So str=str does not cause any error and any change! since you have an already declared name within the method, class variable str is hiden, the only way to access it is with this.str
hope it helps!!!
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops!!! null is printed because every reference variable declared, which is a class variable, not a local variable, is initialized with null, since it's value did not change, it prints null.
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sunitha,
The question is intersting. How ever the method local variable doesn't come in to picture when you call t.str and this.str. As both of them look for the varibale in the object scope.
[ March 01, 2003: Message edited by: Sarma Lolla ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic