• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

A question about Inner Class

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stevie,
That means then non static Inner Classes CANNOT declare static variables by themselves unless they are final.
Amol
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !
reply
    Bookmark Topic Watch Topic
  • New Topic