• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • 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?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic