• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

shadowed variables

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[1] class Foo{
[2] Bar myBar= new Bar();
[3] void changeIt(Bar myBar)
[4] {
[5] myBar.barNum=99;
[6] System.out.println("myBar.barNum in changeIt is " + myBar.barNum);
[7] myBar=new Bar();
[8] myBar.barNum=420;
[9] System.out.println("myBar.barNum in changeIt is now" + myBar.barNum);
[10] )
[11] public static void main(String [] args) {
[12] Foo f=new Foo();
[13] System.out.println("f.myBar.barNum is " + f.myBar.barNum);
[14] f.changeIt(f.myBar);
[15] System.out.println("f.myBar.barNum after changeIt is " + f.myBar.barNum);
[16] }
[17] }

class Bar{
int barNum=28;
}

Can anyone Please explain the lines [6],[9],[13],[14] and [15].Thank you.
[ October 09, 2007: Message edited by: Thirumalai Muthu ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Before line 9 all instance variables gets initialized.

So, at line 9 it prints 28.

When it calls f.changeIt(f.myBar) at line 14, it is passing the same the value of address where reference for object initialized at line 2 is stored.

According to rules when modification of any value of object happens then it gets stored at same address, but if new object is created then the result is stored at different address.

So,
Line 6 prints the modified value 99.
Line 9 will print 420 for the new myBar object.

And line 15 will print 99 because the modified value of passed myBar object has changed to 99 at line 5.

Hope this is clear.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Before line 9 all instance variables gets initialized.

So, at line 9 it prints 28.



In the above sentence it should be line 13 and not 9... rest of the explanation seems fine.
 
Thirumalai Muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Vishal and krithika.I understood.
 
Legend has it that if you rub the right tiny ad, a genie comes out.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic