permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Double-standard for inner class if it is non-static vs if it is static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Double-standard for inner class if it is non-static vs if it is static" Watch "Double-standard for inner class if it is non-static vs if it is static" New topic
Author

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

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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?

Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Double-standard for inner class if it is non-static vs if it is static
 
Similar Threads
Anonymous Class
Please help
final local variable question
Inner Class Final vaiable reference
automatic variables availability?