• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static modifier doubt

 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class outer {
class inner{
static int a;
}
}

Please explain why static variable is not allowed inside the class inner.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sentil,
Looking at JLS (�8.1.2),following can be concluded
[1]An inner class is a nested class that is not explicitly or implicitly declared static unlike member inner interface which are always implicitly static.
[2]Inner classes may not declare anything static like static initializers or member interfaces and static members unless they are compile time constants fields.
[3]Inner classes may inherit static members that are not compile-time constants even though they may not declare them.

Below is a code which demonstrates all the above statements.



Hope this will help.
[ November 18, 2006: Message edited by: Sanjeev Kumar Singh ]
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reason is because,

Static Nested class can have static memebers, because it can accessed using it's class name.

For example,



But this is not the case for Inner class,

inner class properties and methods can only be accessed through its instance.

Outer.inner inClassInstance = new Outer().new inner();

inClassInstance.<any of its properties or class members>;

But static should be accessed through it Class Name and not through its specific instances. Eventhough compiler allows in some cases.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed Prabhu,

Can you please explain why an inner class may inherits static members?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjeev,

Perhaps I can try to do Prabhu's job at least partial. I do not know the exact answer, but in the case of inherited static members, an outer instance is not necessary. You can access the static fields via the class names:


prints "Outer".


I think for non-static inner classes it must be so, that if they inherit from a super class, they must inherit everything, regardless if the fields are static or not.
But it won't compile if you make new static fields (or hide / "override" the inherited ones).

But you are right, it's a bit against the logic.


Yours,
Bu.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bu,
but in the case of inherited static members, an outer instance is not necessary. You can access the static fields via the class names

This quote is really very important.For the time being I am burning this concept what Prabhu described and you replied.
Hey why you have written override
But it won't compile if you make new static fields (or hide / "override" the inherited ones).

Static methods can be made hidden only not overriden.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic