Cannot refer to the non-final local variable foo defined in an enclosing scope

Hi,

When I build my projects using the dockerized SDK Tool, I have the following errors:

Cannot refer to the non-final local variable foo defined in an enclosing scope

However, I don’t have such errors when I build directly from the SDK (4.1.5).

What is going on?

Best,

Hi,

The difference of behavior is due to a non-conforming java compliance level in your SDK configuration.

The Java compliance level of MicroEJ Application is Java 1.7. (official doc)
This is the default proposed by the templates in SDK 5.

To change the Java compliance level, go to Window > Preferences > Java > Compiler and select the compliance level 1.7 and rebuild the projects.

forum-compliance-level

To fix the error itself, the variable must be final.
For example:

int foo = 0xdeadbeef;
Runnable r = new Runnable() {

	@Override
	public void run() {
		System.out.println(foo); // error because foo is non-final
	}
};

Instead, mark the variable as final:

final int foo = 0xdeadbeef;
Runnable r = new Runnable() {

	@Override
	public void run() {
		System.out.println(foo); // OK because foo is final
	}
};

Best,

1 Like