- what is a good why non-static inner classes cannot have static fields, methods, or classes ? - why can local classes only access final local variables, event though they can access non-final fields of the containing class ?
It is helpful at this point to abuse the terminology somewhat, and say, loosely, that the static keyword always marks a "top-level" construct (variable, method, or class), which is never subject to an enclosing instance. This shows why an inner class cannot declare a static member, because the entire body of the inner class is in the scope of one or more enclosing instances.
2) Actually what the JLS says is
Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final, and must be definitely assigned (�16) before the body of the inner class.
Instances can live longer than methods. If an inner class accessed a non-final variable that was declared in a method of the outer class (making it a local variable of the method), when the method that creates the inner class instance ends, that variable will not be available any more. By making inner classes only use final variables of the outer class method the compiler can turn the variables into constants so it doesn't matter when the local variable goes out of scope.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Non-static inner classes can only exist if an instance of the surrounding class exists. Static fields, methods, and classes can exist without an instance of the class that surrounds them existing. These rules cancel each other out, so the static modifier is not allowed in non-static inner classes.
Local classes can only access final local variables, and can also access fields of the containing class event though they are not final because the way a local class accesses it's references... a surrounding class will always exist when a local class exists, so any fields of the surrounding class can be accessed at any time... however, local variables disappear when the method goes off the stack... if a local method stuck around too long, the method that enclosed it could have ended ( it's hard to think that this could happen, but maybe threads or slow garbage collection could cause it... ), and it's references to local variables could point anywhere... so the final modifier is used.... a final modifier means that the value contained in that variable will not change, so the local inner class can make a copy of the contents of the local variable and not have to worry about it changing and never have to refer directly back to the local variable and chance it having disappeared.
P.S. Oh, yeah... you might want to check out the Java Ranch Naming Policy... [This message has been edited by Nathan Pruett (edited May 14, 2001).]
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joined: Sep 29, 2000
Oh yeah, and artur a, Please change your name to be compliant with JavaRanch's naming policy. Your ID should be 2 separate names with more than 1 letter each. It should not be obviously fictitious. We really want this to be a professional forum and would prefer that you use your REAL name. Thanks, Cindy