Hello All, Could someone kindly explain with an example the below paragraph related to Static Nested Classes.
One can define a class as a static member of any top-level class. Classes which are static class members and classes which are package members are both called top-level classes. They differ from inner classes in a sense that a top-level class can make direct use only of its own instance variables.
I'm confused now Thanks in advance. Kind Regards. Hasnain Javed Khan.
Please refer to "Example 7.4 Accessing Members in Enclosing Context" in Khalid Mughal Book.
Joined: Jun 04, 2007
Here, Boo is a static member of class Foo. And as we know, even if there is no object in the heap for a particular class, the static members do exist.. Existence of a static member doesn't depend on the existence of an Object..
So 'a' solely depends on the exixtence of an object whereas Boo doesnt... It is a static member, meaning, it can be accessed even if theres no object of the class..
So how can it access the "a"? -Not possible rite.. (There may jus be no object, and so no "a" after all...)
That's about the access part.
So what have we gained by this, by having a static nested class? 1)We are showing it explicitly that Boo is related to Foo Good way to code right..
2)We can have another class Boo, like
and, there's just no name-conflict.
The first Boo is Foo.Boo and this second one is just Boo