File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static modifier doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static modifier doubt" Watch "static modifier doubt" New topic
Author

static modifier doubt

sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
class outer {
class inner{
static int a;
}
}

Please explain why static variable is not allowed inside the class inner.
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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 ]

~Sanjeev Singh<br />SCJP 1.5
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

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.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Agreed Prabhu,

Can you please explain why an inner class may inherits static members?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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.
 
 
subject: static modifier doubt
 
Similar Threads
Question about static class
no static method in inner class - why ?
Top-level classes
doubt on Inner class
is inner class can be static . what is the purpose of inner class ??