• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About static member of inner class

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No inner class can have a static member.
But why does this code compile?
public class AAA{
static class A{
static int i=0;
}
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because a "static top level nested class" by definition is NOT an inner class (even though it is inside another class).

From the JLS


8.1.2 Inner Classes and Enclosing Instances
An inner class is a nested class that is not explicitly or implicitly declared static.


 
Kate Shaw
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Thanks!
It is said by SL-275 that: method of a static inner class do not have access to the outer class scope.
Does it mean this one do not compile:
class AAA {
static int i;
static class A{
static void a(){System.out.println(i);}
}
}
But actually, it compiles.
I want to know why. Thanks.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kate
A static inner class can access static members of its enclosing class but it cant access non-static members of the enclosing class.
check this code out for an example:

hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Kate Shaw
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it mean that the static variables is not included in the the outer class scope?
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say that static variables are within a classes scope (they have to be within a classes scope to even exist), but just like all other things labeled static, they are independent of any objects that are created from the class, and can be accessed without having to have an object created. A static member can only directly access other static members.
That's why your static inner class can only access other static members. They are, however, still within the scope of the outer class (within the first and last braces).
Clear as mud, eh?
Jason
 
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic