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

Jaworski- local inner class

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
}
}
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic