• 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

static block with forward refrences.

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this question also discuss in javaranch but didn't clearify

how is giving output first code-6,7 and second code-66,77












 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ritesh raushan wrote:this question also discuss in javaranch but didn't clearify . . .

That can’t be true. No, no, no, no, no.

This is JavaRanch.

Instance members do not have an order, but static blocks do. Joshua Bloch says, in Effective Java, says that multiple static blocks (well, actually initialisers) are a potent source of confusion. Because they have an order, the field x is initialised to 66 and later to 6, in the first example. Executed in the order the code was declared in.
In the second example, it is the other way round.
You can read about static initialisers in the Java Language Specification; I haven’t read that section for a long time. I think I quoted the right bit. I do not know whether multiple instance initialisers are also executed in the order they are declared.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java Language Specification links I posted earlier might not be as helpful as I thought they would.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this JLS section, but it is difficult to read.
reply
    Bookmark Topic Watch Topic
  • New Topic