This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initializer expression execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initializer expression execution" Watch "Initializer expression execution" New topic
Author

Initializer expression execution

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Please look at the code below:

It compiles fine, now instead of "this.i" i replace with i, and instead of Initializers1.k i have k,
It gives compiler errors in the lines.
This code would give compiler errors:


Please let me know any suitable explanation for such behaviour
Thanks
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
static members are resolved (and thus initialised) in the order they are declared.
As you're declaring k only after its first use in the initialisation of z you're getting an error.
In the first case you're getting a forward reference which is resolved at runtime, in the second it's (attempted to be) resolved at compile time.


42
 
GeeCON Prague 2014
 
subject: Initializer expression execution