wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jaworski- local inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jaworski- local inner class" Watch "Jaworski- local inner class" New topic
Author

Jaworski- local inner class

lstrite
Greenhorn

Joined: Jul 10, 2000
Posts: 20
In the Jaworski book it states:
"Since local inner classes are not class members, they are not tied to an instance of the outer class"
If that is the case, then why are they able to access nonfinal instance variables such as in this example, which compiles fine?
I would think that they have to be associated with an instance of the outer class in order to access outerNonfinalVar.
class Test{
final int outerFinalVar=1;
int outerNonfinalVar=1;
static int outerStaticVar=1;
final static int outerFinalStaticVar=1;
void aMethod() {

class Inner {
void innerMethod() {
int r;
r=outerFinalVar;
r=outerNonfinalVar;
r=outerStaticVar;
r=outerFinalStaticVar;
}
}
}
}
chetan nain
Ranch Hand

Joined: Jun 21, 2000
Posts: 159
istrite,
the local inner class you are referring to is implicitly non-static. (this is because its defined in the context of a non static method). as you know, a non static method can access static as well as non static members. in this context, we are referring to a local inner class. thus, the inner class is able to access static as well as non static variables (final int outerFinalVar=1) of enclosing class.
hope this helps
lstrite
Greenhorn

Joined: Jul 10, 2000
Posts: 20
So is Jaworski's statement "Since local inner classes are not class members, they are not tied to an instance of the outer class" untrue then? I understand why my code works, but I don't understand his statement.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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).]
lstrite
Greenhorn

Joined: Jul 10, 2000
Posts: 20
maha,
Thanks for the detailed explanation, I understand it much better now. Thanks for your time!
I seem to get hung up on these type of questions, and not really understanding what the question is asking. I hope the exam questions are clearer!
Thanks,
Lisa
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Lisa,
Don't worry. You will do definitly good. The real Exam questions are clear and straightforward. Do you know one thing ? There are many phases in 'any learning process'. When we don't know anything at all, we don't understand whether we are correct or the other person is correct. Then , we slowly get to know the picture of what's really happening. Now atleast we know what the other person is talking about though we may not understand everything in detail. Next phase is we try to analyze ourselves, coin our own opinions on others statements. This is the hard-phase. Now we will get into arguments, sometimes a sense of achievement, sometime frustration etc. This is the phase we should keep on trying. In this phase we learn a lot. I think you are in this phase. When we try to find any ambiguious, nit-picky words means we are lot better than the first phase. Which also means we have a better knowlegde to analyze on our own and say which is correct and not correct and we do not take things for granted. In this phase we have to have patience to appreciate others arguments and points and don't leave anything half way. When we overcome this 3rd phase we can take a challange on own own. We have a mindset now.
I think everybody including me went through this process, somtimes getting frustrated with why the authors are asking like that. Why should I remember the operator precedence like that? But eventually we will all come to terms. Also do this. Whenever you feel something wrong feel free to post here to know what others think. This is a very good way of learning experience I think. Good luck for your 3rd and 4th phase.
regds
maha anna
 
Consider Paul's rocket mass heater.
 
subject: Jaworski- local inner class
 
Similar Threads
Inner class and Methods
local classes..............
Regarding Local Inner Classes
Anonymous Inner class
local class in a static method