• 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

About Block Initializer

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm interested in the sequence of variable(static or instance) declarations and their block initializers (static or instance respectively).
Is the declaration executed earlier then the block even it is declared later? Such as following code,

According to JLS8.3.2.3, http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#287406
it seems the above code will compile without error, but when I compile it under 1.4, it says "illegal forward reference". In JLS it also has a long code as follow,

I compile the above code with error lines commented, it fails too. So now I'm confused, or have I misunderstood the JLS?
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I understand, static initializers are executed first in the order they've been placed in the code. Instance initializers are only executed (also in order) when you create an instance of the class.
[ July 21, 2003: Message edited by: Andres Gonzalez ]
 
Amanda Fu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply! But how about the execute order of initializer and declaration?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amanda:
Thanks for your reply! But how about the execute order of initializer and declaration?


Sorry but I did not get your question :roll:
 
Amanda Fu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As my understanding, a static block cannot make a forward reference to static variables that they are defined after its definition. So I cannot understand the above code in JLS, if we only concern the static variable x, remove all others unrelated lines,

I regard even the first line: x=100 will not compile. But JLS says it is "Ok", that's where I'm confused.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


it seems the above code will compile without error, but when I compile it under 1.4, it says "illegal forward reference".


Do you use an IDE?
I have no problem in compiling this by command-line with SDK1.4...
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I compiled the code successfully with SDK 1.4
 
reply
    Bookmark Topic Watch Topic
  • New Topic