aspose file tools*
The moose likes Beginning Java and the fly likes weird questions, need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "weird questions, need help " Watch "weird questions, need help " New topic
Author

weird questions, need help

artur a
Greenhorn

Joined: May 14, 2001
Posts: 1
- 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 ?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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).]


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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.

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
Don't get me started about those stupid light bulbs.
 
subject: weird questions, need help