File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class Fundamentals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class Fundamentals" Watch "Inner class Fundamentals" New topic

Inner class Fundamentals

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
I would like to know why Inner class has a limitation that member variable should be compile time constant.
What is the problem if the variable i in the
given code can be changed afterword(i.e not a compile time constant).
class TopClass{
class Inner{
//Error, Inner classes cannot have static declaration.
static int i=10;//Error
static final int i=20;//OK.compile time constant.

Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Harwinder,
Q: Why are inner classes not allowed to have static members (except constants)?
First some background info for perspective to appreciate the question and the answer.
Before I came to the JavaRanch I tried to learn from the experienced and talented folks at the Sun Advanced Java forum. This particular question came up time and time again. They spent pages and page, hours and hours debating and guessing. No one there presented a convincing reason. Do a search there or here and you will find lots of guesses and fuzzy reasons.
There are no clues in the JLS or The Java Programming Language.
One day, in the middle of someone else�s post on nested classes, I inserted the famous question: (I wish someone could explain why inner classes cannot declare static members.)
Jim Yingst presented a convincing explanation. As far as I am concerned, Jim walks on water. Save his answer, you may never find another nearly as good. It takes some patience to work through his answer. But that�s due to the nature of the question.
inner classes and static members
My question is at June 27 2:33 PM. Jim�s answer begins at June 29 3:08 PM.
[ January 14, 2004: Message edited by: Marlene Miller ]
Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
Thanks Marlene.
I agree. Here's the link:
subject: Inner class Fundamentals
jQuery in Action, 3rd edition