It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static inner class " Watch "static inner class " New topic
Author

static inner class

weiquan yuan
Greenhorn

Joined: Jun 13, 2006
Posts: 5
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

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..........


KARTHICK.C , SCJP6-93%
(Born to Win)
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Just try:

And the rules for static can be applied.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static inner class
 
Similar Threads
Please clarify why the following code works - Method Local Inner classes
how to start a thread??
Problems with threads, PLEASE IGNORE MY PREVIOUS TOPICS
Doubt in threading
thread