This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about Static Nested Class

 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Javed,

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



Regards,
Gitesh
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic