• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner class Fundamentals

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
}
}
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marlene.
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic