• 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

Inner class

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Outter
{
public static class Inner
{
public static void InnerMethod()
{
}
}
}
Which statement will correctly instantiates an instance of Inner outside of outter
a> Inner m = new Inner();
b> Outter.Inner oi = new Inner();
c> Outter o = new Outter();
Outter.Inner oi = o.Inner();
d> Outter.Inner oi = new Outter.new Inner();
e> Outter.Inner oi = newOutter.Inner();

Please tell me the answer for this
I marked e as the correct answer.
But Please also tell me the difference between e and c option.

Regards
Patrick
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In class Outer you have class Inner which is a static member.
So, the fully qualified name of Inner class is Outer.Inner & hence this is used to get the reference of Inner class. Now u need not require an object to access class-level/static variables. So we can write as follws

Outer.Inner oi = Outer.new Inner() will work.

But u can access static members with an object of the class also.So

Outer o = new Outer(); Outer.Inner oi = o.new Inner(); also works.

Thats why answer c is right.

But accrocing to Java there is no such type of expression like
newOuter.Inner();
If u want this way u can say
new Outer().new Inner();
So, thats why e is wrong.

I hope u got it.
Ok ,bye
 
Patrick Punty
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the option e is correct and C is wrong.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!!!

Outer.Inner oi = Outer.new Inner() //this doesnt work!!!
and also i dont see who it will the inner class has no connectionto the outer class....

similarly the below statement doesnt work
Outer ob = new Outer()
Outer.inner ob = ob.new inner();//the inner class has no connection with the outer class so calling outer_obj.new Inner doesnt seem right!!!

So i thing only Outer.inner obj = new Outer.inner(); is right

Please correct me if i am wrong!!!

Regards
Simon
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The option "e" is correct. The reason is as follows:

The static Inner class is finally a top level nested class and it is as such not a member of the enclosing class.
So to instantiate an inner class we need to have the full path of the inner class i.e. Enclosing class.StaticInnerClasses()
Hence the correct way to instantiate the static inner class is :

Outer.Inner ie = new Outer.Inner() //The fully qualifed path of the inner class
Using a reference of enclosing class for static inner class is incorrect.

If the package till enclosing class is imported then the inner class can be instantiated as

import Outer.*;
Inner i =new Inner();

Let me lknoow if this helps.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
patrick,

for instantiating an non-static class

Outer.Inner i=new Outer().new Inner (); is one way


and for instantiating an non-static class

Outer.Inner i=new Outer.Inner();


thanks
sri
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srikanth:
You made an error - You put the words "non-static" for both lines of code. Which is which? I'm guessing the 2nd code is the static one. Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic