• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused about Static Nested Class

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic