By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

Local Class Question

P: n/a
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?

Thanks,
Tim

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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?


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(final int i) {
return new Foo() {
public void print() {
System.out.println("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

Jul 17 '05 #2

P: n/a
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" <rd******@hold-the-spam.twcny.rr.com> wrote in message
news:BT******************@twister.nyroc.rr.com...
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?


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(final int i) {
return new Foo() {
public void print() {
System.out.println("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

Jul 17 '05 #3

P: n/a
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" <no****@span.com> wrote in message
news:xMfrb.108286$9E1.537659@attbi_s52...
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?

Thanks,
Tim

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.