Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How Nested Static Class method include this?

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In k&B book it is written that

"The class isn't really static there is no such thing as a static class. The static modifier in this case says that the nested class is a static member of the outer class "

My question is since nested class is a static member of the outer class How it is legal to use 'this' inside its method?? I know that nested static class methods are not static but don't you think that it is invalid according to above statement??


 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static Inner classes are static members of outer class and the methods of static inner class are not necessarily to be static unless we do not specify explicitly.
We have to use new operator to create an object of such class so it is legal to use "this" within such classes because here "this" will refer to current instance of Static inner class.
We can not use "this" within static method of such class.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My question is since nested class is a static member of the outer class



nested static class is a static member not static method of outsider class. I think you are taking this statement as "Nested static class is a static method of outsider class.
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What could be the advantage of static Nested class?
In which scenario should anyone create it? I mean since its methods are not static, its instance variables are not static. It do not have any relationship with its outer class. So in which part of code we use it?
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched google and found one very good use of inner classes.

We can use static inner classes for debugging purpose.



when we compile outer class,compiler creates two classes outer.class and outer$inner.class

We can use inner class for debugging during development and when we need to deploy or ship the application we can ship outer.class only.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!