• 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

inner member class & the enclosing class

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand JLS 15.9.2, case: inner member class.
(1) Warm-up Example:
class A { class C {} }
class B extends A {}
In this example, C is an inner member class. C is a member of A. B extends A. So C is also a member of B.
C is declared in A. So A is a lexically enclosing class of C. Is B a lexically enclosing class of C? I don�t think so.
(2) Warm-Up Example:
class A { class B { class C {} } }
In this example, C is an inner member class. A and B are both lexically enclosing classes of C. B is the innermost lexically enclosing class of C.
C is a member of B. C is not a member of A.
(3) How can an inner member class be a *member* of more than one lexically enclosing class?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I don�t understand the JLS words taken literally. But I think this is what they are *trying* to say:

C is an inner member class of Outer.
new C() appears in class B.
Outer is the 2nd lexically enclosing class of B.
�this� is a reference to a B object.
The enclosing instance of C is the 2nd enclosing instance of �this�.
[ April 24, 2003: Message edited by: Marlene Miller ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an interesting line from the JLS: Otherwise, if C is a member of an enclosing class then let O be the innermost lexically enclosing class of which C is a member,
it sounds to me from reading this that a class is a member of all classes in its inner class hierarchy.
"Inner class hierarchy" is a term I stole from Douglas Dunn, the author of "Java Rules". He describes an inner class hierarchy as being the relationship between inner classes and the top level class in which they are declared.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Thomas for the reference to Java Rules. You mentioned it to me once before. I think I will look for it.
That interesting line is *exactly* the line I am trying to understand. I balk at the word *innermost* since I can only imagine *one* enclosing class of which C is a *member*.
class A { class B { class C {} } }
I am surprised *if* C is a member of A.
[ April 24, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS has 3 authors. Maybe one author wrote 8.2 and another author wrote 15.9.2.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this example, A is a lexically enclosing class of both C and E. But A is not the innermost lexically enclosing class of C.

Therefore I think that line in JLS 15.9.2 should say EITHER
let O be the enclosing class of which C is a *member*, OR
let O be the *innermost* lexically enclosing class of C.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not agree Marlene.


let O be the enclosing class of which C is a *member*, OR
let O be the *innermost* lexically enclosing class of C.


These options are not a real disjunctive. Both mean that O is the enclosing class of C, and this is the point. JLS should not mention the difference because is of no importance.
Your code shows what happens when the class O does not enclose the class where the instance creation expression occurrs.
I think we are talking about this:


Otherwise, C is an inner member class (�8.5).
* If the class instance creation expression is an unqualified class instance creation expression, then:
o If the class instance creation expression occurs in a static context, then a compile-time error occurs.
o Otherwise, if C is a member of an enclosing class then let O be the innermost lexically enclosing class of which C is a member, and let n be an integer such that O is the nth lexically enclosing class of the class in which the class instance creation expression appears. The immediately enclosing instance of i is the nth lexically enclosing instance of this.
o Otherwise, a compile-time error occurs.
* Otherwise, the class instance creation expression is a qualified class instance creation expression. The immediately enclosing instance of i is the object that is the value of the Primary expression.


If so, the following translate to code the previous JLS paragraph.

If I have understood correctly the paragraph, it says that the class declaring the inner class must also enclose the class where the instance creation expression occurs; also that, the immediately enclosing class of the instance created is not the immediately enclosing instance of this but the instance of the class that declared C (O).
By the way n is two in my example.
__________________________________________________
Again an edition of a post deletes part of it
[ April 24, 2003: Message edited by: Jose Botella ]
[ April 24, 2003: Message edited by: Jose Botella ]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get "Java Rules" for $6 in the amazon marketpalce!!! It is one of the best books (if not the best book) for helping to explain the JLS. I can't speak highly enough about how good this book is.
[ April 24, 2003: Message edited by: Thomas Paul ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jose for considering my question. What you have said makes sense.
Now, consider the clause �let O be the innermost lexically enclosing class of which C is a member�. *innermost* suggests there might be other lexically enclosing classes of which C is a *member*.
In your example, Test, Test1, Test2 and O are enclosing classes of C. O is the innermost enclosing class of C.
C is a member of O. But C is not a member of Test, Test1 and Test2.
I claim there is only *one* lexically enclosing class of which C is a *member*. *innermost* does not make sense.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I finally understand 15.9.2 � the case for inner member classes and unqualified class instance creation expressions (although I have forgotten now why I wanted to know).
The enclosing instance of the new class is in the comment.

Is this correct?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I claim there is only *one* lexically enclosing class of which C is a *member*. *innermost* does not make sense.


Yes I agree. And your example sounds correct to me.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your time and help, Jose and Thomas.
(I just remembered why I got interested in this problem. But I'll save that for another post.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic