• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Local classes

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks! this one is regarding local classes. The code described below gives compiler error "Local class not found".
<code>
class BigClass{
public static void main(String args[]){
new Local();
final String s = "some local results";
class Local{
Local(){
System.out.println(s);
}
}

}
}
</code>
but the following code compiles and prints "some local results"
<code>
class InnerClass1 {
public static void main(String args[]){
final String s = "some local results";
class Local{
Local(){
System.out.println(s);
}
}
new Local();
}
}
</code>
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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howard, didn't your question answer itself?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
6.3 Scope of a Simple Name
The scope of a parameter of a method (�8.4.1) is the entire body of the method.
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:
Its own initializer
Any further declarators to the right in the ForInit part of the for statement
The Expression and ForUpdate parts of the for statement
The contained Statement
The scope of a parameter of an exception handler that is declared in a catch clause of a try statement (�14.18) is the entire block associated with the catch.
These rules imply that declarations of class and interface types need not appear before uses of the types.
In the example:
<pre>
package points;
class Point {
int x, y;
PointList list;
Point next;
}
class PointList {
Point first;
}
</pre>
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
maha anna
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic