• 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:

When does an initialization block run?

 
Ranch Hand
Posts: 218
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does an initialization block run before or after statements in a class defining and maybe initializing instance variables?

This code compiles & runs, printing "1". Line 3, where x is defined, must have run before the init block, otherwise I'd expect an error at line 2 when the init block attempts to access a variable x that doesn't exist. So, given that the init block must have executed after line 3, how come the program doesn't print out "2"?

 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They run in textual order.



Now you get 2.

Not very intuitive, indeed. int x = 1; will de facto be split int int x; and x = 1 and executed at separate times; the declaration when needed (before the initializer), the assignment in textual order.
 
Richard Hayward
Ranch Hand
Posts: 218
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Hauke.

Hauke Ingmar Schmidt wrote:They run in textual order.

Not very intuitive, indeed. int x = 1; will de facto be split int int x; and x = 1 and executed at separate times; the declaration when needed (before the initializer), the assignment in textual order.



I thought it must be doing something weird like that.

So my code

is somehow reorganized to this

All such assignments happening before any constructor runs.
 
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Hauke Ingmar Schmidt

"They run in textual order" what does this mean?
 
Richard Hayward
Ranch Hand
Posts: 218
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gajendra Kangokar wrote:@Hauke Ingmar Schmidt

"They run in textual order" what does this mean?



The order in which the statements are encountered in the source code, except that, in lines such as

int x =1;

variable declarations are separated from the assignment part of the statement, and executed first.

Seems weird.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Hayward wrote:
The order in which the statements are encountered in the source code, except that, in lines such as

int x =1;

variable declarations are separated from the assignment part of the statement, and executed first.

Seems weird.



Since this is in reference to runtime, I think it is better to think about when memory is allocated (and zeroed) for instance variables, and when they are initialized. The JLS regards, initializations of instance variables, and instance initializer blocks, running in "textual order"... but obviously, you can't allocate the memory for the instance variable fields at that point -- it needs to be allocated much earlier than that.

Henry


BTW, it is not everyday that the original poster, who ask the question, is the one who follows up to help out others with the same issue (a month later). Nice. You earned a cow.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic