I had to do some research on that pesky final keyword. Here's what I now
understand:

Use final for classes that should not be extended and methods that should
not be overridden (all methods of a final class are implicitly final). If
you use final in an attempt to increase performance, you lose two ways: it
won't improve performance and it will confuse the real purpose of the final
keyword for classes and methods. It also may make the code less extensible..

Use final for class or instance fields that should be immutable for the
lifetime of the class or instance. Again, not for performance, but to insure
your code works correctly.

For method parameters and local variables, use final to insure the variable
is not changed after it is initialized.

Note that final protects primitive type VALUES, but for other types it only
protects the REFERENCE to the object, not the object's value (unless, of
course, the object itself is immutable, like java.lang.String).

You can (and often do) define an inner class, either private or anonymous,
within a method. Methods in the inner class can access parameters or local
variables of the enclosing method only if those variables are final
(immutable). This is due to some "smoke and mirrors" that occurs at runtime
when the inner class is instantiated. The inner class actually contains
copies of the method's parameters and variables that it needs which are
initialized when the inner class object is instantiated. This is done
internally, invisible to the programmer. I suppose the theory for this is
that once the method and the inner class object part ways (the inner class
object's methods may be run on a different thread than that used to
instantiate it) the semantic binding of the variables is easier to
understand if they are immutable. However this leaves a question: even
though the bound variable must refer to the same object (because the
references are immutable), what if the value of the object is changed? Would
the bound object need thread-safe methods? I don't know - maybe a Java Guru
can comment on these questions.

Two more things: 1) The final keyword helps the compiler detect programming
errors, like when you try to reassign a value to an immutable variable. 2)
In general, use final when you must, not when you can. I've seen some
overzealous use. Add a comment describing why final is necessary.
Programmers who read your code later on will scratch their heads if you used
final for no good reason.

In closing, on a (perhaps) simpler note, it's interesting to compare Java
inner classes to Javascript closures. In Javascript closures, the bound
variables are not immutable. I've often run into this problem in a loop when
I bind to the loop variable in a closure. Later on, when the closure is
executed, it gets the last value of the bound variable instead of the value
when the closure was defined.

References:

http://www.ibm.com/developerworks/java/library/j-jtp1029.html
http://renaud.waldura.com/doc/java/final-keyword.shtml
http://www.codeguru.com/java/tij/tij0071.shtml

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to