I am new to java and have been scratching my head understanding some its concepts.
I am following the tutorial Java tutorial. However, I cannot find the usefulness of using Static Nested Classes. I mean I think I need some good examples as to why I should want to use it. Can someone provided me some codes as examples so I can understand it better?
thax
I am new to java and have been scratching my head understanding some its
Share
The benefit of a static nested class over an “ordinary” class is that you can use it to reflect the relationship between two classes.
For example in the JDK there is
java.util.Mapandjava.util.Map.Entry.java.util.Map.Entryis declared as apublic static interfaceand doing it this way clearly signposts its relationship toMap. It could have been defined asjava.util.MapEntrybut doing it as a static nested interface makes it clear that it has a strong relationship toMap.So you’d probably only use static nested class when the nested class would only ever be used in the context of its parent.