wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Classes Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Classes Variables" Watch "Inner Classes Variables" New topic
Author

Inner Classes Variables

Vicky Nag
Ranch Hand

Joined: Jul 02, 2003
Posts: 40
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
Uma Balu
Ranch Hand

Joined: Aug 22, 2003
Posts: 61
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.
krishna madala
Greenhorn

Joined: Nov 18, 2003
Posts: 12
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

Joined: Jul 02, 2003
Posts: 40
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
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
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

Joined: Jul 02, 2003
Posts: 40
I think the program below would solve the ways inner classes can be instaiantiated. Correct me if I am wrong.

Regards
Vicky
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inner Classes Variables
 
Similar Threads
confused question
Private Access Q
static modifiers
inner classes
Mock exam Q- Which variables can an inner class access?