A question about Inner Class
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 24 years ago
Hi Amol...,
Non-Static Inner Class are "allowed to access" static variables, but they can NOT have a static member variables by them-self, unless they are compile-time-constant, so in your code, since class Inner extends class StaticVar, then Inner can access variables j in class StaticVar, hence the output is 100.
hth
stevie
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Amol Keskar:
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"?
Hi Amol...,
Non-Static Inner Class are "allowed to access" static variables, but they can NOT have a static member variables by them-self, unless they are compile-time-constant, so in your code, since class Inner extends class StaticVar, then Inner can access variables j in class StaticVar, hence the output is 100.
hth
stevie
Amol Keskar
Greenhorn
Posts: 23
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Stevie,
That means then non static Inner Classes CANNOT declare static variables by themselves unless they are final.
Amol
That means then non static Inner Classes CANNOT declare static variables by themselves unless they are final.
Amol
Stevie Kaligis
Ranch Hand
Posts: 400
posted 24 years ago
True !
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Amol Keskar:
Thanks Stevie,
That means then non static Inner Classes CANNOT declare static variables by themselves unless they are final.
Amol
True !
| They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |






