Hi,
I have a question about Inner Classes. I know that Non-Static Inner Classes cannot have static member variables UNLESS they are compile time constants (final variables) and that static Inner Class can have a static variable. But in the following code
in the case of NON-STATIC InnerClass "Inner" compiler does not complain about having access to static var "j"?
Can someone explain to me the fundamental reason behind this?
Does this mean that non-static inner class cannot declare static variables of its own (unless final) but can inherited from its superclass?
class StaticVar
{
static int j = 100;
}
class Outer
{
class Inner extends StaticVar
{
static final int x = 3;// ok compile-time const
//static int y = 4; // compile-time error
Inner()
{
System.out.println("" +j);
//Has access to static var j of StaticVar class
}
}
static class AnotherStaticNested
{
static int z = 5; // ok, not an inner class
}
public static void main(
String[] arg)
{
Outer o = new Outer();
Outer.Inner i = o.new Inner(); //prints the value of 100
}
}
Thanks very much
Amol