• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

question about static inner classes

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which statement about static inner classes is true?
A. An anonymous class can be declared as static.
B. A static inner class cannot be a static member of the outer class.
C. A static inner class does not require an instance of the enclosing class.
D. Instance members of a static inner class can be referenced using the class name of the static inner
class.
The right answer is C
But I want to know why the answer D is not correct?
Thank u very much
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think D is not correct because a static inner class acts just like any other non-nested (or top-level) class except that its type and accesibility are defined by its enclosing class meaning it is accessible only if its enclosing class is. Besides that static nested classes are members of their enclosing class(or interface) and therefore they can access any members of the enclosing type (including private).
What I was trying to say is that declaring a nested class static doesn't make its members static so that you can access them using the class name like with the regular static variables or methods.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
A static inner class can define both static and non-static members. Since non-static member requires an object instance, hence they cannot be referenced using the Class name. On the other hand the static members can be accessed using Class name.
Treat a static inner class as a package-level class and you will find all your answers...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic