posted 8 months ago
[Update: I didn't see Stephan's reply until I posted this, but mine is in a similar vein...]
I imagine the Java compiler does more than one pass of a class in order to compile it, since it needs to be able to know what all the members of a class are in order to be able to reference them out of order. They presumably build that list of members on one pass, and then on a second pass, use that to determine whether a given method access or field access expression is valid, and if so, what to connect it to. But apparently, they didn't choose to apply this to allow multiple passes within a method. Perhaps because they didn't think it would be particularly worthwhile to do so. I'm sure they could have if they really wanted to - but why would it be useful?
I would argue that aside from technical questions of whether and how they could do this, there's a question of whether it's a good idea. I think the most relevant concern here, to me, is whether it results in more readable code, or not. Personally I would find such out-of-order declarations harder to read. Maybe it's just me - readability is often all about what you're used to, after all. But I appreciate things that limit the amount of other code I need to read in order to understand a given bit of code. Declaring things before they are used, for local variables and local types, is one of those things.