This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Why do we need static nested class?  RSS feed

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we use non static inner classes when we dont expect an object to be created independent of a specific (top-level class) object. for example : we cant have an Event Object without its associated GUI Component object.therefore its better to have an event handler within the Component.

But why do we need to make an inner class static because then it dishonours the association rule. we dont need the outer class object to create the inner class object. it can exist independently.and if the static inner class object can exist independent of outer class then why not have a seperate class whose object can be created independently ??? Can some one provide a simple example to suggest its use and advantages
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is already a discussion about this here : http://www.coderanch.com/forums/posts/preList/461717/2061282
 
shukla raghav
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. i am afraid the link you suggested talks about what are static nested classes. I want to know what is the ideal situation in which we would resort to using a static inner class.

2. somwhere i read the following lines regarding use of static inner class. i could not understand what it means:-

The advantage of a static nested class is that it doesn't need an object of the containing class to work. This can help you to reduce the number of objects your application creates at runtime.

 
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called a nested class. All nested classes are implicitly static; if they are not static they are called inner classes.
You can have a class which has some part of itself and other classes can instantiate that part. The one example which springs to mind is Map and Map.Entry. You can have a Map with K-V pairs or you can get an Entry which is a single K-V pair.


That isn't quite as easy as it looks because Map and Map.Entry are actually interfaces not classes. So somebody else may have a better explanation.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right that static nested classes aren't really needed. However, you could make the argument that if two classes are only used together -- such as class A uses helper class B, and B is never used independently -- then a static nested class is the way to go. In other words, by making B just a regular old class, the programmer who stumbles across it may try to use it. If B were a static nested class, then it's more clear that it has a special use that relates to A.

I also use static nested classes a lot with generics, although I'm not sure if that's an appropriate use. In this case, if I have a class Customer, I may create a public static class within Customer such as: public static class CustomerList extends ArrayList<Customer>{}. Since CustomerList is only used in the context of Customer, it seems logical to define it within Customer.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and if the static inner class object can exist independent of outer class then why not have a seperate class whose object can be created independently ???



The general use of static inner classes is if they only make sense in combination with the enclosing class. Instead of putting it into it's own file, you use a static inner class.

At least that's what I do.

Same applies for enums, you would not put an enum into a separate class when it only makes sense to use it with some other class.
 
shukla raghav
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i understand there is very limited use of static nested classes but then how does it save creation of unnecessary objects i am still not understanding.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps the javasoft best describe it with the following:

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

and here is the link to it http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

and i agree with your statement as well. unless the term "static class" means differently.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shukla raghav wrote:i understand there is very limited use of static nested classes but then how does it save creation of unnecessary objects i am still not understanding.


It doesn't.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are  four types of Static Nested Classes:

1) an outer class has a static nested class
2) an outer interface has a static nested class
3) an outer class has a static nested interface
4) an outer interface has a static nested interface


More on this subject can be found at
Static Nested Classes
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and what about enums? I am not convinced it is that helpful for your tutorial to say there are four kinds of static nested class. I would add public and private nested classes to your types, and you no longer have four.
 
Ranch Foreman
Posts: 3254
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I would add public and private



Pretty sure there are more options here for access modifiers.    Aside from that, you can also declare a static nested interface, or declare a static nested type inside another nested type.   Or even declare a static type inside an annotation - though offhand I can't imagine a reason why.  None of this stuff changes the essentials of how static nested types work, except when you need to know that something is implicitly static, or implicitly public.  So yes, it seems silly to try to say there are just four types since (a) there are many more than four, and (b) the differences between those different "types" aren't particularly important.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static nested classes are not really inner classes; their sole purpose is to serve structuring and scoping mechanism for logically related types.
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!