aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static variables and forward reference Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Static variables and forward reference" Watch "Static variables and forward reference" New topic
Author

Static variables and forward reference

Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142


In the code snippet above, how come X could be referred in the static block, even though it is only declared afterwards? I thought all static code is executed sequentially from top to bottom. How does the compiler know about X at //1 ?

Thanks.
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9191
    
    2

You can forward reference variables to assign value to them. You cannot forward reference to read their values. So your code is legal but this code would not be legal


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Static variables and forward reference
 
Similar Threads
please explain the output
why is the below code not printing 2 i agree its a post increment but its being assigned to x???
What does evaluation mean? post, pre ++ --
SCJP Operator question
++/-- operator