this should not refer to the lambda

Alex Blewitt alex.blewitt at gmail.com
Sun Feb 21 17:06:46 PST 2010


On 22 Feb 2010, at 00:53, Mark Mahieu wrote:

> class Foo {             
> 	Runnable r = new Runnable() {
> 		public void run() {
> 			r.run();
> 		}
> 	};
> }

Thanks for both Mark and Vladimir for correcting me. 

I note though the example above works because the field (r.run()) is actually a field reference to the instance of the enclosing class; and the field of the class is known and defined (statically) whilst the instance is bound by a dynamic lookup at runtime. 

If it's converted inside an instance method, then the code fails to compile:

class Foo {             
 	void bar() {
		final Runnable r = new Runnable() {
			public void run() {
				r.run();
			}
		};
	}
}

Foo.java:5: variable r might not have been initialized
			r.run();
			^
1 error

However, I do accept that it is possible to define initialisers in Java which are self-referential; so I withdraw my earlier objections to the issue of recursive lambdas at that ground.

Alex


More information about the lambda-dev mailing list