Re: Local Class Question
From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 11/09/03
- Next message: Xela: "Re: Creating BufferedImage"
- Previous message: Filip Larsen: "Re: Local Class Question"
- In reply to: Tim Hill: "Local Class Question"
- Next in thread: Tim Hill: "Re: Local Class Question"
- Reply: Tim Hill: "Re: Local Class Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 09 Nov 2003 17:18:36 -0500
Tim Hill wrote:
> I'm just getting up to speed on Java, and have a question about local class
> behavior. Here's a mini-app to illustrate:
>
> interface Foo { void print(); }
>
> class LocalClassTest
> {
> public static void main(String[] args) {
> Foo foo = getFooImpl(100);
> foo.print();
> }
>
> public static Foo getFooImpl(final int i) {
> class FooImpl implements Foo {
> public void print() {
> System.out.println("i = " + i);
> }
> }
> return new FooImpl();
> }
> }
>
> As expected (at least, as I expect) this code prints "i = 100". But what
> bugs me is where does this come from? By the time I call "foo.print" the
> method "getFooImpl" has returned, and its local variables/parameters should
> have been flushed off the stack, along with the value "100" that eventually
> gets printed. The only way I can see this as working is that the compiler
> has somehow promoted the parameter "i" in "getFooImpl" into a hidden field
> within class FooImpl, but I cannot find any docs about this anywhere. Or is
> "i" really a reference and has FooImpl grabbed a shadow(y) ref to it?
>
> Any of you experts care to shed some light on this?
The Inner Classes Specification is available as part of the JDK 1.1
documentation, and perhaps elsewhere as well. It describes this
particular kind of situation specifically. Similar to your guess, the
compiler creates a synthetic field inside the inner class into which
the value in question is written. Because the variable is final in the
containing method, there is no worry that the value of the copy will
ever differ from the original. The local variable in the containing
method is distinct from the copy in the inner class instance, however,
and is not in fact special in any way not reflected by its declaration.
It _is_ necessary for variables referenced in this way to be final, but
I suspect you already realize that that does not necessarilly make them
compile-time constants. The variable in your example certainly isn't
one. The key, though, is that there should be no externally detectable
difference in behavior from what would be observed if the containing
method's local variable were somehow retained and used directly.
John Bollinger
jobollin@indiana.edu
- Next message: Xela: "Re: Creating BufferedImage"
- Previous message: Filip Larsen: "Re: Local Class Question"
- In reply to: Tim Hill: "Local Class Question"
- Next in thread: Tim Hill: "Re: Local Class Question"
- Reply: Tim Hill: "Re: Local Class Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|