Local variables used but not declared in an inner class must bedeclared final.

P

Philipp

Hello,
In JLS 8.1.3, where inner classes are specified, we read "Any local
variable, formal method parameter or exception handler parameter used
but not declared in an inner class must be declared final.".
Why is this needed?
As far as I can see, as a programmer, you can always get around of
this limitation by making a final copy of the variable (see code
below).

public class Test {
public void doSomething(){
String a = "hello";
final String b = a;

Runnable r = new Runnable(){
public void run() {
System.out.println(a); // compile error, but would be OK with
b
}
};
}}

As a side question. Do you generally declare all your local variables
final when this is possible? Is there a substantial gain in doing
this?

Phil
 
O

Owen Jacobson

Hello,
In JLS 8.1.3, where inner classes are specified, we read "Any local
variable, formal method parameter or exception handler parameter used
but not declared in an inner class must be declared final.".
Why is this needed?
As far as I can see, as a programmer, you can always get around of
this limitation by making a final copy of the variable (see code
below).

public class Test {
  public void doSomething(){
    String a = "hello";
    final String b = a;

    Runnable r = new Runnable(){
      public void run() {
        System.out.println(a); // compile error, but would be OK with
b
      }
    };

}}

As a side question. Do you generally declare all your local variables
final when this is possible? Is there a substantial gain in doing
this?

"Because the alternative might be confusing."

Consider:

public void force(Runnable r) {
r.run();
}

public Runnable create () {
int x = 0;
return new Runnable () {
public void run () {
x = 1;
}
};
System.out.println (x);
}

public void useTheForce () {
force (create ());
}

Assuming it were allowed, what would you expect x = 1 inside the
Runnable to do?

It's a complex problem addressed in many ways by other languages.
Google for "closure" for a lot of insight.

-o
 
L

Lew

Philipp said:
As a side question. Do you generally declare all your local variables
final when this is possible?

No, but I do for those where it's needed.
Is there a substantial gain in doing this?

The purpose of 'final' on a local variable is to lock down its value
to one unchangeable. There's a substantial gain to doing this where a
variable must not change its value once allocated; it enforces the
unchangeability at compile time. Catching bugs at compile time is an
order of magnitude less expensive (effortful) than at run time,
according to all the literature I've seen on the matter.

The 'final' attribute is necessary when an inner class accesses the
variable, because the inner class requires the guarantee of
immutability. The benefit there is enormous - the program won't even
compile without it.

There's much less gain if a variable just happens not to change value
and enforcement is unnecessary or trivial. A three-line method that
you can see at a glance doesn't change a variable's value won't run
any faster if you declare the variable 'final', but at least you'll be
secure that any attempt to change the value will crash at compile
time, even though there are no such attempts.

Yet.
 
R

Roedy Green

In JLS 8.1.3, where inner classes are specified, we read "Any local
variable, formal method parameter or exception handler parameter used
but not declared in an inner class must be declared final.".
Why is this needed?

see http://mindprod.com/jgloss/nestedclasses.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
"Humanity is conducting an unintended, uncontrolled, globally pervasive experiment
whose ultimate consequences could be second only to global nuclear war."
~ Environment Canada (The Canadian equivalent of the EPA on global warming)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top