• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Inner Classes Variables

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Outer
{
private static class StaticInner
{
}
private class NonStaticInner
{
}
public static void main(String args[])
{
StaticInner in1 = Outer.new StaticInner(); // Line 1, compiler error
Outer.StaticInner in2 = Outer.new StaticInner(); // Line 2, compiler error
NonStaticInner in3 = new Outer().new NonStaticInner(); // Line 3
Outer.NonStaticInner in4 = new Outer().new NonStaticInner(); //Line 4
}
}
Line 1 and Line 2 gives compiler error. I donot understand why??
Line 3 and Line 4 defines variables for Inner classes in 2 different ways and it compiles ok. I donot understand the difference between the two.
Can any body help please..
Regards
Vicky
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vicky,

This gives a compilation error, 'coz the usage of new is not correct here, since, the inner class that you are trying to access is a static member, the usage should be
StaticInner in1 = new Outer.StaticInner() ;

The above explanation holds good here also. Since the instance of the static member is created in the same class, the class prefix, Outer is not needed.

The instance of a non-static inner class should be created only with a valid outer class instance. What you are trying to do is creating a anonymous instance of outer class and then using that instance to create a non-static inner class instance. This can also be done as

Hope this helps you.
Thanks,
Uma.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Outer
{
static Integer a = 10;
static class Inner
{
}
}
how do you access 'a' above Outer.a right, so inner static class is no different, you need to call Outer.Inner, so to instantaite u'll say new Outer.Inner().
 
Vicky Nag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NonStaticInner in3 = new Outer().new NonStaticInner(); // Line3 Outer.NonStaticInner in4 = new Outer().new NonStaticInner(); //Line 4
Thanx for the explanations.
What about
NonStaticInner in3
and
Outer.NonStaticInner in4
How are they different or are they both the same .. Confused.
Regards
Vicky
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vicky
You have a very 'reasonable' doubt. The syntax to instantiate top-level nested classes and non-static inner classes is kind of inconsistent (this is my *personal* opinion and I maybe wrong).
Uma's explanation was perfect.
I have a comment on this statement by Krishna though:


how do you access 'a' above Outer.a right, so inner static class is no different, you need to call Outer.Inner, so to instantaite u'll say new Outer.Inner().


It's a matter of syntax and you cannot "intuitively" guess the expression to instantiate non-static inner classes by looking at the expression to instantiate top level nested classes.
Consider the following piece of code:

If we were to apply the same analogy here:
How do you access 'b' above: 'o.b' right? Then why couldn't you create the non-static inner class instance as new o.InnerNonStatic()?
(Compiler will treat 'o' as a package name in the line 1 above since it won't be able to find a class 'o' in scope).
Thanks
Harwinder
 
Vicky Nag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the program below would solve the ways inner classes can be instaiantiated. Correct me if I am wrong.

Regards
Vicky
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic