lstrite,
What you say is correct. But not in this context.
Since local inner classes are not class members, they are not tied to an instance of the outer class - means this. All local inner classes whether they are defined inside a method or another code block like the instance floating block/static floating block are known ONLY to the block of code within which they are defined. Nowhere else.
What the author means is, IN ORDER
TO CREATE AN INSTANCE OF THE LOCAL INNER CLASS , WE DO NOT NEED AN INSTANCE OF THE OUTER CLASS. On the other hand, to create an instance of an inner class, we need an instance of the outer class but not for a local inner class. Please look at the following example.
<pre>
class Test {
class InnerClass {}
public static void main(String[] args) {
class Local_InnerClass {}
Local_InnerClass l1 = new Local_InnerClass(); //This is ok.
//InnerClass inn1 = new InnerClass(); //Not ok
InnerClass inn2 = new Test().new InnerClass(); // Ok
}
}
</pre>
The author is talking about whether we need an instance of the outer class in order to create an instance of the local inner class or not. Also note that just because an local inner class is able to access all the instance variables it DOES NOT mean it is associated (tied) with an instance of the outer class.
Logically thinking, an inner class defined inside a static method is also another type of local inner class right?. Now this local inner class can access only the static vars of the outer class. It can't access the instance vars of the outer class. So can we change our previous statement and say, "NO No , local inner class is not tied with an instance of the outer class" ?
Both statements are contradicting. Isn't? So the concept which you used to prove the author's statement otherwise, which is " since local inner class can access the non-final and instance vars of the outer class it is tied with an instane of the outer class" , is NOT quite appropriate.
This also proves that the author is just talking about to
create an instance of a local inner class, whether we need an instance of the outer class or not.
Also note that, in case of accessing vars , for a local inner class, we can generalize the statement like this.
WHATEVER vars available to the method inside which the local inner class is defined + all the final vars inside the method + all the final parameters to the method. 'WHATEVER' here means if it is a instance method, then the local inner class can access all instance and static vars of the outer class. If it is a static method, it can access only static vars of the outer class. Do you get the point. Accesessing of outer vars does not play a role in the concept of whether we need an instance of the outer class to create an instance of a local inner class or not.
By this statment '
tied with an instance of an outer class', the author just means, an instance of a local inner class DOES NOT depend on an instance of the outer class.
So the author is correct in what he means.
regds
maha anna
[This message has been edited by maha anna (edited July 27, 2000).]