Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Doubt on static initialiser

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider following code :

Initially I thought the answer would be compiler error as the variable was referenced before it was declared but it prints 5 as variable y is a static variable.
Now consider following code where static initialiser block is deliberately placed on top :

Now the output is 10.
Can anyone please explain me why it prints 10 instead of 5 as the static initialiser would be called after the static variable was declared and initialised.
 
Saloon Keeper
Posts: 10422
223
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static initializers are run in the order they are encountered in the code. In your second code snippet, y = 10 comes after y = 5, so at the end of class initialization, y == 10.
 
Neha Agnihotri
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Static initializers are run in the order they are encountered in the code. In your second code snippet, y = 10 comes after y = 5, so at the end of class initialization, y == 10.



Thanks for your reply Sir. Does it mean line 8 in the second code snippet gets executed twice by the parser?
 
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Neha Agnihotri wrote:Please consider following code :

Initially I thought the answer would be compiler error as the variable was referenced before it was declared but it prints 5 as variable y is a static variable.
Now consider following code where static initialiser block is deliberately placed on top :

Now the output is 10.
Can anyone please explain me why it prints 10 instead of 5 as the static initialiser would be called after the static variable was declared and initialised.

. Neha
When the class is loaded into memory static fields and static initializes are excuited in the order they are specified,, so here first field is initialized as it is first placed and then static initializer, so y=5;
And then when object is created instance field is initialized to value which is assigned to x=y;
So it contains value 5
In second code first static block is excuited as it is placed first, assignment to static field is OK before declaration in static block,
Again static field is initialized to 10 so it contains value 10 therfore 10 is printed
Hope it clears
I suggest you to read about declaration before reading rule in initializes expression
 
Stephan van Hulst
Saloon Keeper
Posts: 10422
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Neha Agnihotri wrote:Does it mean line 8 in the second code snippet gets executed twice by the parser?


Probably not.

The compiler parses the code once, which results in an "abstract syntax tree". In this tree, the compiler will first look up variable declarations, and only after all variables have been declared will it consider the initializers. By the time the compiler has parsed the code into a syntax tree, it no longer knows on what line variables were declared and on what line initializers were written.

The only relevant thing it remembers by then is the relative order of the initializers, so the virtual machine will run initializers that appear earlier in the code before initializers that appear later.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!