• 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

Duplicate variables and initialization blocks

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


prints in the following order:
aaa
bbb
ccc
bbb

To someone who doesn't know what's going on, this looks pretty strange.

First question: why does it appear the compiler allows a to be declared a second time by String a = "ccc" in the initializer block?

I venture to guess the answer to that question might resolve the second question:
why is it the value bbb that remains for the variable a as illustrated in the println from main()?
Intuitively for me, a should equal ccc in the main() method.

Many thanks.

Matt


 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im not sure, but i guess its because instance variable 'a' when declared first time, on object creation it get stored with object in heap. Inside the initialization block 'a' start referring new string 'bbb'. When declaration of 'a' appear for the second time, it gets stored in stack because it is being declared in initialization block. When initialization block ends, reference variable 'a' gets destroyed and string 'ccc' goes to string pool with no reference. The reference variable 'a' stored with object is still referring 'bbb'. So bbb gets printed in main. I think this is the reason.
 
matt love
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Astha.

Sounds good to me.

Matt

 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

matt love wrote:

First question: why does it appear the compiler allows a to be declared a second time by String a = "ccc" in the initializer block?



The variable is within a block so it's a new variable.

Interestingly, Initializing Instance Members says -

The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.



Regards,
Dan
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The line



is a new local variable a and in no way refers to the instance member with the same name. This also explains why the instance member has the value "bbb" when the final line of the main method is executed.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic