Local Class Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Hill

    Local Class Question

    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(fina l int i) {
    class FooImpl implements Foo {
    public void print() {
    System.out.prin tln("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?

    Thanks,
    Tim



  • Raymond DeCampo

    #2
    Re: Local Class Question

    Tim Hill wrote:[color=blue]
    > 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(fina l int i) {
    > class FooImpl implements Foo {
    > public void print() {
    > System.out.prin tln("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?
    >[/color]

    Tim,

    You've hit the nail on the head with your first explanation. The
    compiler does indeed turn the local variable i into a instance variable
    on the FooImpl class.

    Something to keep in mind is that all of this local and inner class
    stuff is done 100% by the compiler. The JVM does not know anything
    about it. So when an inner class does something that an ordinary class
    would not be able to do (like access private data of the outer class)
    the compiler adds methods and instance variables as it sees fit to make
    this work.

    Also, local classes aren't really commonly used. More common is the
    anonymous class. Here's you example re-written as an anonymous class:

    interface Foo { void print(); }

    class LocalClassTest
    {
    public static void main(String[] args) {
    Foo foo = getFooImpl(100) ;
    foo.print();
    }

    public static Foo getFooImpl(fina l int i) {
    return new Foo() {
    public void print() {
    System.out.prin tln("i = " + i);
    }
    };
    }
    }

    If you are just beginning with Java, I wouldn't recommend focusing on
    inner and local classes at first. They have quite a few nuances.

    HTH,
    Ray

    Comment

    • Tim Hill

      #3
      Re: Local Class Question

      Thanks Ray, good to know I'm not going mad. I'd also investigated the anon
      class for this behavior using exactly the example you showed!

      Also, I agree with the general advice about not getting into these fiddly
      fringe things -- just filling in my knowledge of Java (I'm a C++ veteran, so
      not too scared of the wrinkles, which compared to C++ are thankfully few and
      far between in Java :)

      -Tim

      "Raymond DeCampo" <rdecampo@hol d-the-spam.twcny.rr.c om> wrote in message
      news:BTyrb.3502 4$1N3.9613@twis ter.nyroc.rr.co m...[color=blue]
      > Tim Hill wrote:[color=green]
      > > I'm just getting up to speed on Java, and have a question about local[/color][/color]
      class[color=blue][color=green]
      > > 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(fina l int i) {
      > > class FooImpl implements Foo {
      > > public void print() {
      > > System.out.prin tln("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[/color][/color]
      should[color=blue][color=green]
      > > have been flushed off the stack, along with the value "100" that[/color][/color]
      eventually[color=blue][color=green]
      > > gets printed. The only way I can see this as working is that the[/color][/color]
      compiler[color=blue][color=green]
      > > has somehow promoted the parameter "i" in "getFooImpl " into a hidden[/color][/color]
      field[color=blue][color=green]
      > > within class FooImpl, but I cannot find any docs about this anywhere. Or[/color][/color]
      is[color=blue][color=green]
      > > "i" really a reference and has FooImpl grabbed a shadow(y) ref to it?
      > >[/color]
      >
      > Tim,
      >
      > You've hit the nail on the head with your first explanation. The
      > compiler does indeed turn the local variable i into a instance variable
      > on the FooImpl class.
      >
      > Something to keep in mind is that all of this local and inner class
      > stuff is done 100% by the compiler. The JVM does not know anything
      > about it. So when an inner class does something that an ordinary class
      > would not be able to do (like access private data of the outer class)
      > the compiler adds methods and instance variables as it sees fit to make
      > this work.
      >
      > Also, local classes aren't really commonly used. More common is the
      > anonymous class. Here's you example re-written as an anonymous class:
      >
      > interface Foo { void print(); }
      >
      > class LocalClassTest
      > {
      > public static void main(String[] args) {
      > Foo foo = getFooImpl(100) ;
      > foo.print();
      > }
      >
      > public static Foo getFooImpl(fina l int i) {
      > return new Foo() {
      > public void print() {
      > System.out.prin tln("i = " + i);
      > }
      > };
      > }
      > }
      >
      > If you are just beginning with Java, I wouldn't recommend focusing on
      > inner and local classes at first. They have quite a few nuances.
      >
      > HTH,
      > Ray
      >[/color]


      Comment

      • James

        #4
        Re: Local Class Question

        Just to add a bit more, an method inner class as you have below can only
        access method variables that are declared final. By declaring them final
        they are not existing on the stack in this case.
        "Tim Hill" <nospan@span.co m> wrote in message
        news:xMfrb.1082 86$9E1.537659@a ttbi_s52...[color=blue]
        > I'm just getting up to speed on Java, and have a question about local[/color]
        class[color=blue]
        > 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(fina l int i) {
        > class FooImpl implements Foo {
        > public void print() {
        > System.out.prin tln("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[/color]
        should[color=blue]
        > have been flushed off the stack, along with the value "100" that[/color]
        eventually[color=blue]
        > 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[/color]
        is[color=blue]
        > "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?
        >
        > Thanks,
        > Tim
        >
        >
        >[/color]


        Comment

        Working...