• 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

weird questions, need help

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- 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 ?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic