Author | Topic: Local classes |
Howard Stern ranch hand |
posted March 18, 2000 03:56 PM
Hi folks! this one is regarding local classes. The code described below gives compiler error "Local class not found".
but the following code compiles and prints "some local results"
The boldface part has been the only modification done. Why does the instantiation work if it is declared after defining the localclass. Is there some enlightenment around the corner? Thanks
|
Jane Rozen ranch hand |
posted March 19, 2000 02:33 PM
Howard, didn't your question answer itself?
|
Howard Stern ranch hand |
posted March 19, 2000 08:20 PM
Hi Jane, Well I did work out the code and post the question 'coz I did not understand how exactly the compiler works and why it doesn't see the class. As it is evident although I know how the program works I could not explain that to myself and hence the question. It is a little more than what is evident. Neither it is explained anywhere in the JLS (I did do a search!). If the local class was a subclass then there would not have been any problem. I have still got no explanation.
|
Jim Yingst sheriff |
posted March 19, 2000 09:11 PM
Howard- inner classes didn't exist when the JLS was first written, and their supposed official definition is in the inner classes specification, which unfortunately is a poorly-organized piece of crap and doen't seem to answer your question. Fortunately, Sun is in the process of creating a second edition of the JLS; you can see the draft edition here. Unfortunately it's only available as one big PDF file at the moment, but that will eventually change. It contains much clearer rules for inner classes, including: "The scope of a local class declared in a block is the rest of the immediately enclosing block, including its own class declaration". (Section 14.3) "The rest" means it's in scope after the declaration, but not before. It's analogous to a local variable - you have to declare it before you can use it. Clear?
|
maha anna bartender |
posted March 20, 2000 07:12 AM
This is some extra info to add to what Jim said. The main point to see here , when a name is referenced is if the name is accessed within its scope. The scope of a simple name has the foll rules. From JLS The scope of a parameter of a constructor (§8.6.1) is the entire body of the constructor. The scope of a local variable declaration in a block (§14.3.2) is the rest of the block in which the declaration appears, starting with its own initializer (§14.3) and including any further declarators to the right in the local variable declaration statement. The scope of a local variable declared in the ForInit part of a for statement (§14.12) includes all of the following: These rules imply that declarations of class and interface types need not appear before uses of the types.
the use of PointList in class Point is correct, because the scope of the class type name PointList includes both class Point and class PointList, as well as any other type declarations in other compilation units of package points. regds
|
| | |