• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Use of static Variables and static initializers

 
Ranch Hand
Posts: 47
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi;)

I am currently studying for the OCAJP I Certification and as I am working myself through
a lot of exam questions I stumbled upon this one:



It asks what the output is. Possible answers are:
- 2
- 4
- 5
- 6
- Compilation fails

I tried it out, and the answer is 5. I don't exactly get why, though...

What I can see here is that the statics are initialized when the Class is loaded.
First, x is asigned the value of 2, and z is declared as static.

Then, below the main() Method, in a static-Block, a new Variable x is defined, with the value 3.
Seems, it kind of "hides" the original Class-Variable x defined before the
main() Method. After that, z gets assigned the Value of x. But NOT the Value of the "new" x
(the one below the main() Method)! Instead, it uses the Value of the "hidden" x (the one
before the main() Method).
The System.out.println(x+z) then prints 5.

Why is this?

Kind regards;)

Florian
 
Bartender
Posts: 3993
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Florian, welcome to the ranch!

change your static initializer to this:

and the outcome will be clear.
 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I see what you did there Piet;)

So in the static Block a complete new Variable x get's defined, which is only valid in that Block,
am I right? Then z gets assigned the Value of that new x, which is 3. In the main() Method
the Class.x (the one before the main() Method) gets used.
I hope I got it all right;)

Thank you for your help!

Florian
 
Ranch Hand
Posts: 459
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's about shadowing variables.

If in static block don't declare int x , x will be static variable x.

It's about 2 variables, one static and another local.

If declare a int z variable  in static block , the result will be 2.

 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much;)
 
Piet Souris
Bartender
Posts: 3993
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Florin,

sorry I missed your reply. It was, as Dana explained, spot on. Have a cow!
 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you;)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic