• 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

Anonymous Local Variables

 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So having not used anonymous inner classes all that much in practice other than to add a listener in swing, I was surprised to discover the existence of anonymous local variables.

For review, those are local variables that must be marked as final, that an anonymous inner class can essentially borrow and save a reference to. In the case that I was working, the original reference for the object was lost but the inner class, which never declared the variable but simply used it, was able to keep access to.

Why is such a thing allowed? And also, what's the proper way to call this anonymous variable outside the class? Can reflection be used? What modifiers are attached to the object such as private/public, etc.

Really, what bothers me about it is it feels like sloppy programming. If you are going to pass a variable to a class, it should be passed explicitly as an argument -or- at the very least stored in the class as a private member variable. I don't like the idea of a java class automatically creating an instance member variable for every final local variable available.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Anonymous local variables." Did you invent that term or did you read it somewhere? I haven't heard it before, nor do I understand it -- such variables have names and aren't themselves special in any way. The only thing that's special is what the compiler does when you reference them in an anonymous inner class.

Under the hood, the compiler transforms this:



into something (conceptually) like



Local variables that are accessed by an anonymous class get turned into members in that class, and they're passed as synthesized constructor arguments. The reason you're only allowed to do this with finals is to complete the illusion that you're accessing the locals directly, but in fact, you're not: you're accessing copies made at the moment the anonymous inner class object is constructed. No extra copies are made anywhere.

In contrast to your negative opinion, I think this is rather elegant. Being able to access locals in the enclosing function lets you keep your anonymous inner classes very small.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, I just made up the term. Is there a real term for these variables?

I enjoy the comfort of accessing parent members inside regular inner classes, I just didn't know the same held true for anonymous inner classes with local variables. I guess for coding purposes it makes sense and shortens a lot of work, it just seems odd to me that there exists an instance that can access a variable, never directly declared on that instance, that I've long since thrown away. At the very least, it seems a place for potential memory leaks.
[ February 04, 2006: Message edited by: Scott Selikoff ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Selikoff:
Yep, I just made up the term. Is there a real term for these variables?



No, I don't think so. I never thought of them as a discrete category before you brought this up!

At the very least, it seems a place for potential memory leaks.



That's a good point; I can imagine someone not realizing that an anonymous class was holding onto an object via this mechanism -- and you can't null them out the way you can null out normal members!
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing I forgot to ask. Does the anonymous inner class make copies of all references to final local variables, or only the ones that it specifically uses. I would think the latter, but I'd like to know this for sure.
[ February 06, 2006: Message edited by: Scott Selikoff ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only the ones it uses.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a demo. At first I could not see any generated fields becaue in my first attempt they were like invisible, and the compiler decided to fold the constants in...
 
The only thing that kept the leeches off of me was 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