The purpose of the final keyword is not for performance reasons, and using it as such makes for a bad design. Sometimes we can live with a little bad design if it makes a big difference in performance, but I don't think this is the case anymore, and the final keyword should be used appropriately. There are some good valid reasons to use final, such as:
final variables:
Guarantees that the variable does not get changed somewhere. This is good for constants to make sure they stay constant, and allows variables to be used within inner classes because a copy can be made and kept with the object.
final classes:
Guarantees that a immutable class remains immutable. This allows sharing of references to an object without copying it, and guarantees a certain behavior for the object, which therefore provides a sort of "pass by value" behavior. This is why String and the primitive objects such as Integer and Double are declared final. Otherwise you could simply subclass the object and do what you want, which would mean that you would have to make a copy of every String that you wanted to hold on to.
final methods:
Guarantees that a subclass does not change the behavior of one of your methods.
Just my thoughts, Chris
Santiago Pericas-Geertsen wrote:
----- Original Message ----- From: "Joseph Kesselman" <[EMAIL PROTECTED]>
Note that final methods also cause problems if/when you need to introduce
subclassing/polymorphism, and that SUN's own performance team generally
recommends strongly *AGAINST* trying to use the final keyword as an
optimization trick.
Sun's Hotspots has a mechanism by which it essentially infers the final keyword (often, e.g. if a certain class loaded, it can also "uninfer" it). The main advantage of a final method is that it can be inlined, thus saving the overhead of method invocation and late binding. I can't think of any reason for which not to use final: for variables it promotes constant propagation (typically at compile time and when the init expression has not side effects), for methods it promotes inlining.
This is just my opinion though, I can always contact the performance team if we want to make sure. Do you have a reference Joe?
-- Santiago
-- Chris P. McCabe - Principal Engineer Choice Hotels International - Information Systems [EMAIL PROTECTED] 602-953-4416
