• 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

local classes

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why is the line(3) not ok and why is line(5) ok though it is non-staic member.can anybody please explain this?
[ Jess added UBB [CODE] tags to preserve whitespace so the code is more readable ]
[ June 24, 2002: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 5 is acceptable because this inner class extends the class SuperB. Therefore, you're really accessing the member variable x within the inner class instance of StaticLocalE.
Corey
 
madhu avula
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so why line(3) not ok.still did not understand?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, I'm assuming that the method staticMethod is really supposed to be static. Otherwise, we don't have this problem.
The issue is that the variable y, defined within class SuperC, is an instance variable. You can't access an instance variable from a static context...EVER! An instance member requires an instance to be a part of. From a static context, you have no instance for that member to be part of, so you can't access it from there.
The converse is not true. You can access static members from a non-static context.
Corey
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TopLevelClass inherited y from SuperC, but y is not a static variable, so the static local inner class StaticLocalE cannot access y.
reply
    Bookmark Topic Watch Topic
  • New Topic