• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

static inner class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..........
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just try:

And the rules for static can be applied.
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic