• 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

Instance of outer class cannot access static field of static nested class?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I'm trying to figure out why the line below fails to compile (error: unexpected type) ?

If NestedStat is a static member of Test, why can't an instance of Test (myTest) access NestedStat (and then x - being static member of NestedStat) ? I know I could do it by instantiating NestedStat (myNestedStat) and accessing a via myTest.myNestedStat.x , but I want to understand why it doesn't work unless I do that.

Any help would be appreciated. John




 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
myTest is an instance of class Test. In your class definition, you have defined an inner class of Test, called NestedStat, but that definition does not add a member to Test. The "." operator, when it follows a reference to an instance of a class, must be followed by a member or method name of the instance's class. NestedStat is neither, so the compiler is complaining that you have put a variable (myTest) where it requires either a class (Test) or a package (the one Test belongs to, if there is one). The error is a bit confusing, because it is complaining that you have put myTest someplace that it doesn't belong, rather than complaining that NestedStat.x can't go where you've put it (after a dot following an instance variable).

You don't lose anything with this restriction, as, even if it were to work, all variables holding references to instances of Test would get you access to the same single variable, x. Thus, being required to use the outer class name, and not an instance reference, gets you the same thing, with no risk you might be trying to dereference a null, or anything like that.

BTW, this also works:

 
John Mulholland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stevens. Very helpful. I didn't realise that a static nested class is not a member of it's outer class.

Regards, John
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic