Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Significance of static class...  RSS feed

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
About static classes... are the following comments correct?
They can't access instance vars of the outer class (I think they would always be 'sort of' inner classes - always enclosed at least), but they can be instantiated - thereby giving it a this reference for 'internal' use.
:roll: Instantiating a static class just seemed weird to me, but they can be instantiated more than once because they return different hash codes.
So... they behave very much like 'normal' classes except that they basically don't know about the outer class existance (which, I guess technically doesn't ( -have to- ) exist).
Am I missing some details on static classes?
Thanks.
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


They can't access instance vars of the outer class (I think they would always be 'sort of' inner classes - always enclosed at least), but they can be instantiated - thereby giving it a this reference for 'internal' use.


Correct. Because a static inner class can be instantiated without an instance of the enclosing class, it can't reference what may not be there. It may, however, reference a static member of the enclosing class, because they also 'exist' without an instance of the enclosing class. When instantiated, the static inner class has a this reference to itself, much like any other instance of any other instantiated class.


Instantiating a static class just seemed weird to me, but they can be instantiated more than once because they return different hash codes.


Correct again. Because the inner class is static, it can be instantiated without a reference of the enclosing class. Like any other class, everytime you instantiate one you get a new object with a different hash. Unless defined otherwise(ie: static), the members of the static inner class act like the members of a 'regular' class...because it is a regular class, you just happen to be able to create one without an instance of its enclosing class.

in both cases, 2 new objects have been created.
hth
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!