• 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

Nested class access

 
Ranch Hand
Posts: 86
2
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

while playing with arrays I've written this code:

According to The Java Tutorial, static nested classes should not have access to other members of the enclosing class. I'd suppose to get compile-time error in the BasicComparator class. However, my code compiles just fine. Am I missing something?

Thanks.
 
Marshal
Posts: 79263
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which members of the enclosing class did you expect to have difficulty with? The link you provided says static nested classes don't have direct access to instance members of the enclosing class. I can't see that you are directly accessing any members.

Remember o1.value is not access to a member of the class. It is access to a member of the ready‑made object o1. That is different.
 
Tomas Linhart
Ranch Hand
Posts: 86
2
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, at the top of the linked article I read:

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.

I know that static nested class can't access instance members, that's logical to me. But my understanding of the quoted statement is that it also can't access private members through object reference.
 
Campbell Ritchie
Marshal
Posts: 79263
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And about ten lines farther down, it says

And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.

There are errors in the Java® Tutorials, though they usually manage to keep a few pages between successive errors so people don't notice them . This is what it says in the Java® Language Specification (=JLS):-

It is a compile-time error if a static class contains a usage of a non-static member of an enclosing class.

The compiler seems to believe what the JLS says
I think the first quote was supposed to say instance members rather than members.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above should illustrate this.
MyInnerClass can access 'id' directly (as it is part of a MoreTest object).
MyStaticInnerClass cannot (it gives a compilation error).
 
Tomas Linhart
Ranch Hand
Posts: 86
2
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for your patience :-)
I'm aware of the static vs. instance member difference when it comes to access from nested classes. My confusion relates to the access modifier of the value member, and that it's accessible as o1.value in the static nested class method. But I believe now, that this behaviour is correct as the BasicComparator class is a member of SimpleHolder, therefore compiler provides access to other members of the enclosing class (even though via object reference), so it's more a matter of a scope. Do I understand it well? I might have failed in searching, but couldn't find an answer to this.
 
Campbell Ritchie
Marshal
Posts: 79263
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are doing is to provide (a reference to) an instance (o1). If you have a reference to an instance, you can access all its instance members if their access modifier permits you. The scope of private is the whole class (actually I think it is the whole outer class but only 99% sure) so the static nested class is within the scope of private.
 
Tomas Linhart
Ranch Hand
Posts: 86
2
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the explanation I understand :-) Thank you.
 
Campbell Ritchie
Marshal
Posts: 79263
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic