• 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

static nested classes

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain what is happening in this line in detail

MyOuter.MyNested n = new MyOuter.MyNested();
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MyOuter.MyNested n = new MyOuter.MyNested();

MyNested is the static nested class of MyOuter. Hence it's just behaves like a static member of the class MyOuter.
Since to access the static member of the class you need to access it with the enclosing class, same thing is happening here..
to access MyNested you need to enclose it with MyOuter.

Refer Ch-8 K&B for more details
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You are creating object of static member class. The member class is always refered in context of the enclosing class. So when you create a object of that class you create it with this syntax.

OuterClassName.InnerClassName refName = new OuterClassNamesName.StaticInnerClassName();

When a class member is static you can access directly with class name.

In case of non-static , it will be

OuterClassName.InnerClassName refName = new OuterClassNamesName().new StaticInnerClassName();

Thats the difference .

HTH,

Vivian
 
reply
    Bookmark Topic Watch Topic
  • New Topic