• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Inner Classes

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Buddies!
Inner classes can have any level of nesting. What would be the right way to create an instance of inner-most class if we have three levels of nested class like;
class outer
{
class inner
{
class inner1
{}
}
void temp()
{

outer.inner i = new outer.inner();
//Compiler Error outer.inner.inner1 j = new outer.inner.inner();
//Compiler Error inner1 k = new inner1();
}
}
Thanks!
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tanveer Mehmood:
Hi Buddies!
Inner classes can have any level of nesting. What would be the right way to create an instance of inner-most class if we have three levels of nested class like;
class outer
{
class inner
{
class inner1
{}
}
void temp()
{

outer.inner i = new outer.inner();
//Compiler Error outer.inner.inner1 j = new outer.inner.inner();
//Compiler Error inner1 k = new inner1();
}
}
Thanks!


simply
Outer.Inner.Inner1 inner1= new Outer().new Inner1().new Inner2();
or
Outer o=new Outer();
Outer.Inner i=o.new Inner();
Outer.Inner.Inner1 i1=i.new Inner1();

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner classes can have any level of nesting. What would be the right way to create an instance of inner-most class if we have three levels of nested class like;
class outer
{
class inner
{
class inner1
{}
}
void temp()
{
outer.inner i = new outer.inner();
//Compiler Error outer.inner.inner1 j = new outer.inner.inner();
//Compiler Error inner1 k = new inner1();
}
}
Hi tanveer,
Non static inner classes need an instance of outer class.
and since u r creating the instance of inner inside the outer class itself,u dont need to write outer.inner i,this wud also work
inner i=new inner();which is like
inner i=this.new inner() coz non ststic method wud place 'this' implicitly.
to create the instance of inner class u need it to define
inner.inner1 j=new inner().new inner1();
which is equavalent to
inner.inner1 j=this.new inner().new inner1();
hope this will clear your doubt.
 
sandeep bagati
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tanveer Mehmood:
Hi Buddies!
Inner classes can have any level of nesting. What would be the right way to create an instance of inner-most class if we have three levels of nested class like;
class outer
{
class inner
{
class inner1
{}
}
void temp()
{

outer.inner i = new outer.inner();
//Compiler Error outer.inner.inner1 j = new outer.inner.inner();
//Compiler Error inner1 k = new inner1();
}
}
Thanks!


sorry tanveer
I thought u are making an instance outside the outer class.But i think you got the funda.It will still work but got some extra overhead.
 
Tanveer Mehmood
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sundeep and Puneet, I'd my fundamentals wrong. Have cleared the concept....
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic