Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static inner class

 
weiquan yuan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why static inner class can access non static method without compilation error?
here this example has static inner class Forecast, which extends Thread, obviously start is not static method, why it can be accessed in Forecast constructor?

 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static inner classes are only not allowed to access non-static members of the enclosing class directly. Other than that, they are like normal classes (in terms of access to non-static members). So you can access non-static members of the super class in non-static members of the sub-class (which is a static inner class). If you forget that Forecast is a static inner class, would the call to start method in the constructor be illegal??
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
weiquan yuan wrote:why static inner class can access non static method without compilation error?
here this example has static inner class Forecast, which extends Thread, obviously start is not static method, why it can be accessed in Forecast constructor?


it's your wrong interpretation my friend.........

your static inner class ForeCast also extends Thread.......... Remove it and you will get compiler error for sure..........
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just try:

And the rules for static can be applied.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic