Re: inner class scope issues
- From: jrobinss <julien.robinson2@xxxxxxxxx>
- Date: Wed, 25 Mar 2009 02:44:10 -0700 (PDT)
Thanks for taking time to answer, Lew. Even though I must say I'm
still struggling.
Lew answered:
public static class SomeInnerClass extends SomeSuperClass {
It is contradictory to name a static nested class with the word
"Inner". In Java, "inner classes" are non-static member classes.
I know, and I expected your remark. But I wanted to stay consistent
with my first (erroneous) SSCCE.
Of course, the result is the same without "this.".
Wrong, sort of. The reason it fails without 'this' is that the method
call does not have an owning instance.
This is subtly misleading (I didn't say incorrect, mind you!).
You don't have an instance when you're in a static context. Here there
is an instance which is "this", because the method is not static (even
though the nested class is).
Only, the present instance is an instance of the subclass.
I said your remark wasn't incorrect; indeed, the method doesn't have
an "owning instance" because this particlar instance does not own the
method... but I just wanted to point out the difference with "no
instance at all".
You know, to keep things clear for all the lurkers. :-)
#2) we need an instance, because the method is not static (and the
nested class is static): we have super, which is an instance.
Not completely correct. 'super' is this instance, not just any
instance. 'super' means "'this' taken as an instance of the
supertype".
Yes, I expected to be corrected on this. After writing this I tried
out the explicit cast version below, just to verify to myself that I
was going down the wrong path. But I was searching for different
mindsets.
The correct mindset seems to be to think about inheritance of private
members. So, to reformulate...
super.somePrivateMethod(); // works fine!works because we are first accessing an instance of the nesting class,
then calling a private method upon it.
somePrivateMethod(); // compiler errordoesn't work because we're calling a method that doesn't exist ("this"
does not have this method, so this.somePrivateMethod() doesn't exist).
If this is correct, it means that the compiler error is misleading: it
speaks about an access issue, which taken at face value should be
resolved by nesting. I suppose the compiler simply provides the most
helpful message it can, and the scope-based message is generally the
most helpful.
In this particular case, it's not that helpful to say "you can't
access a private member" when on every other line you are! :-)
If I keep this point of view, the access should be ok because it's nested.
So I need to see a subclass as being a superclass, but *without* the
superclass's private members (not with them but with no access).
Not true. The subclass still does have those private superclass
members, it just doesn't inherit them.
That doesn't help me. It's got them, but doesn't inherit them???
Confusing.
Either a class has a method, or it doesn't.
If it doesn't declare a method, the only way to have it is by
inheritance.
OTOH, it may be just a question of words. Do we really care if some
say...
"it doesn't have a method"
....and others say...
"it has a method but doesn't declare it and can't access it"?
The question of access to private members of an outer class is
orthogonal to inheritance, unrelated.
Well, that's more or less what the whole thread is about. :-)
--
JRobinss
.
- Follow-Ups:
- Re: inner class scope issues
- From: Lew
- Re: inner class scope issues
- References:
- inner class scope issues
- From: jrobinss
- Re: inner class scope issues
- From: Mark Space
- Re: inner class scope issues
- From: jrobinss
- Re: inner class scope issues
- From: Lew
- inner class scope issues
- Prev by Date: Re: inner class scope issues
- Next by Date: JEditorPane, html, and image
- Previous by thread: Re: inner class scope issues
- Next by thread: Re: inner class scope issues
- Index(es):
Relevant Pages
|