wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused about Static Nested Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused about Static Nested Class" Watch "Confused about Static Nested Class" New topic
Author

Confused about Static Nested Class

Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
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.
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi,


Here these two classes are top level classes:
Case1:




and, here in this one below, Boo is not a top level class:
Case2:




But whereas here,
Case3:




Boo is a top-level class.
What does it mean here, to say that Boo is top level class?
It cannot access the members of the class Foo.
See that it behaves just like the classes in case1

(Visualize it just like the code in case1)

What use does this have?
NameSpace resolution


Regards,
Vishwa
[ January 17, 2008: Message edited by: Vishwanath Murthi ]

Blog
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Hello Vishwa,
Thank you very much for explaination with examples. Could you kindly elaborate more on

Boo is a top-level class.
What does it mean here, to say that Boo is top level class?
It cannot access the members of the class Foo.
See that it behaves just like the classes in case1

(Visualize it just like the code in case1)

What use does this have?
NameSpace resolution


Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Hi Javed,

Please refer to "Example 7.4 Accessing Members in Enclosing Context" in Khalid Mughal Book.



Regards,
Gitesh
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi,




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

Regards,
Vishwa
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Confused about Static Nested Class
 
Similar Threads
Static Nested Classes
inner class
Top level class
inner classes
Enum withinInner Class