This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class Fundamentals" Watch "Inner class Fundamentals" New topic
Author

Inner class Fundamentals

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
/**
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.
}
}


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: http://aspose.com/file-tools
 
subject: Inner class Fundamentals
 
Similar Threads
inner class-doubt
inner class wording
Static instance variables and static methods
Why inner class can't have static variable
why inner class can't have static modifiers???