Forums Register Login

weird questions, need help

+Pie Number of slices to send: Send
- 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 ?
+Pie Number of slices to send: Send
artur a,
1) Because everything about an inner class is in RELATION to it's outer class. Therefore any "static stuff" that you might want to declare in the inner class needs to belong instead to the outer class.
From the Sun Inner Classes Specification: http://java.sun.com/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc6.html#13908


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.
+Pie Number of slices to send: Send
I'll take a stab at answering your questions....

  1. 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.

  2. 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.



  3. HTH,
    -Nate

    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).]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Please don't post the same message to more than one forum. You obviously expect to read more than one forum - so do the people who might answer your question.
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 764 times.
Similar Threads
field access in local inner classes
Final variables
access modifiers.
final variables in method
Doubt In Static Memeber...Plz Help Me
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:40:52.