Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class Fundamentals

 
Harvinder Singh
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/**
Hi,
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.
}
}
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marlene.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic