Would an inner class by any other name smell less sweet?
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Okay ... if I have a non-static member class such as the following
public class OuterClass
{
private int Blah;
public class InnerClass
{
private int InnerBlah;
}
}
and if I write a static method (say a main method) for OuterClass and wish to construct an
instance of InnerClass then I need to write:
public static void main (String [] args)
{
OuterClass.InnerClass inst=new OuterClass(). new InnerClass();
}
which makes perfect sense to me since the full name of the inner class is not InnerClass but
OuterClass.InnerClass.
But if InnerClass should be a static-member class then the rules change for some reason. Now in
that main method we write:
InnerClass inst=new OuterClass.InnerClass();
The new expression on the right makes perfect sense to me since that is after all the full name of
the inner class. What confuses me is why we can refer to that reference using simply InnerClass
and not OuterClass.InnerClass. I KNOW static classes don�t need an enclosing instance, but that
feature seems moot here. What seems to be missing is the full name of the CLASS rather than a
redundant enclosing instance. Okay I guess I am blathering now. My main question is why in
the above case is it InnerClass inst, rather than OuterClass.InnerClass on the left of the
assignment operator, when the new expression on the right seems to be acting properly (I.e. using
the full name of the inner class ...) *whew*
Dan
public class OuterClass
{
private int Blah;
public class InnerClass
{
private int InnerBlah;
}
}
and if I write a static method (say a main method) for OuterClass and wish to construct an
instance of InnerClass then I need to write:
public static void main (String [] args)
{
OuterClass.InnerClass inst=new OuterClass(). new InnerClass();
}
which makes perfect sense to me since the full name of the inner class is not InnerClass but
OuterClass.InnerClass.
But if InnerClass should be a static-member class then the rules change for some reason. Now in
that main method we write:
InnerClass inst=new OuterClass.InnerClass();
The new expression on the right makes perfect sense to me since that is after all the full name of
the inner class. What confuses me is why we can refer to that reference using simply InnerClass
and not OuterClass.InnerClass. I KNOW static classes don�t need an enclosing instance, but that
feature seems moot here. What seems to be missing is the full name of the CLASS rather than a
redundant enclosing instance. Okay I guess I am blathering now. My main question is why in
the above case is it InnerClass inst, rather than OuterClass.InnerClass on the left of the
assignment operator, when the new expression on the right seems to be acting properly (I.e. using
the full name of the inner class ...) *whew*
Dan
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Dan,
You don't require a outer class inside your main on either side because the JVM knows to find it inside your existing class. The following would also work (besides your example)
But if you are trying to use the static inner class in some other class you would need to supply the outer class part!
Regards,
Manfred.
You don't require a outer class inside your main on either side because the JVM knows to find it inside your existing class. The following would also work (besides your example)
But if you are trying to use the static inner class in some other class you would need to supply the outer class part!
Regards,
Manfred.
Dan Temple
Ranch Hand
Posts: 93
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Okay ... That makes sense.
Thanks for the info!
Dan
Thanks for the info!
Dan
| I need a new interior decorator. This tiny ad just painted every room in my house purple. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |






