• 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

Double-standard for inner class if it is non-static vs if it is static

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enthuware explanation for an answer to a question:



As method inner is an instance method (ie. non-static method), si, ii, and fai are accessible in class Inner. Note that ai is not accessible. If method inner() were a static method, ii would have been inaccessible.




Here method inner is non-static and is able to access si, which is a static variable belonging to class TestClass. I cannot understand the logic for this.
On the other hand if method inner is static, it is notable to access ii, which is an instance variable belonging to an object of TestClass.

And why can't method inner access ai, the automatic variable? Would method inner be able to access ai if method inner was static?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:
Here method inner is non-static and is able to access si, which is a static variable belonging to class TestClass. I cannot understand the logic for this.


A non-static method can access a static member of the class. But, not the other way. A static member can't access a instance member without an instance. Because, static members belongs to Class.

Sandra Bachan wrote:
And why can't method inner access ai, the automatic variable? Would method inner be able to access ai if method inner was static?


The inner method can access the variable ai, but the inner class only can't access it. Check it with a println() statement!
 
reply
    Bookmark Topic Watch Topic
  • New Topic