• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Static Inner class -Doubt ,in K & B

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt in Static Inner Class- Question form K & B Book pg 665.code is :->
class MyOuter{
public static class Inner{
public static void foox(){}
}
}
public class Test11{
public static void main(String args[])
{
//MyOuter.Inner m= new MyOuter.Inner();//correct
MyOuter m=new MyOuter();
MyOuter.Inner mi=m.new MyOuter.Inner();//WRONG
}
}

-------------------------------
In this Question ,we have to tell correct instantion of inner class.

one is ,with outer class'es Name only,no oter instance, needed.

2nd one ,i tried ,its compiler error .but i could nit justfy the reason.
As with static member,s Right way to access is CLASSNAME.VARNAME ...but still not illegal way is OBJNAME.VARNAME
why its not feasibile in staticinner class class.
it outerobj.new inner();
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here class Inner is not an inner class but a nested class.
Inner class: Inner class has access to the members of the outer class and cannot exists without outer class instance.
Static inner classes or it should be more appropriately called static nested class does not meet the condition of the inner class.
Hence to instantiate a static nested class all you need to just instantiate it just like anyother class.
MyOuter.Inner m= new MyOuter.Inner();//correct
Here the refernce is declared as MyOuter.Inner this gives the complete path name of the static nested class Inner and same applies the in the instantiation syntax.
Now when you try
MyOuter.Inner mi=m.new MyOuter.Inner();//WRONG
Is in correct because you are trying to instantiate a static nested class with its outer class instance.

Hope its clear, Just read the difinition of the inner class and static nested class it will be clear.
Thanks
Deepak
 
reply
    Bookmark Topic Watch Topic
  • New Topic