[PJUG Javamail] primitive method argument as primitive and final
Howard Abrams
howard at howardabrams.com
Mon Feb 19 17:33:02 UTC 2007
You are correct in that modern compilers do not need the final
keyword, but to me, the main argument for using the final keyword is
to clarify to yourself what the code should be doing.
I start out, like your generated code here, using final for just
about everything, and then as soon as I add code that attempts to
modify the final variable, my IDE immediately alerts me to the fact
that I'm trying to change my constant. I guess it just gets me to
think twice about my intentions.
Especially, method arguments should be explicitely final.
Of course there are some very practical uses for the final keyword
when it comes to inheritance. Classes that were not designed with
inheritance in mind should be flagged final to avoid problems from
incomplete implementations of those utility methods (especially the
equals(), clone() and finalize() methods).
I liked this guy's summary: http://renaud.waldura.com/doc/java/final-
keyword.shtml
On Feb 19, 2007, at 9:13 AM, Vijay Balakrishnan wrote:
> Hi,
> This is a basic discussion I got into. Is there any value in using
> final with a primitive variable in a method argument in Java ? A
> book mentioned that final makes sense for Class objects.My
> colleagues at work said that it doesn't make any sense to have
> final for primitive variables.(Of Course the new Hotspots etc do
> optimise this code).
>
> A colleague mentioned this:
> In a language like C/C++, it is good to pass const references. In
> a language like Java, it serves little purpose. ???? Is this true ?
>
> Basically , you can read the final arguments, but you can't change it.
>
> public class TestFinalPrimitive {
>
> /**
> *
> */
> public TestFinalPrimitive() {
> super();
> // TODO Auto-generated constructor stub
> }
>
> void finalInt(final int i) {
> //i++;//can't change the value of i in this method..Is
> there any value to using final for i here ?
> }
>
> int readPrimitive(final int i) {
> return i + 1;
> }
>
> public static void main(String[] args) {
> TestFinalPrimitive f = new TestFinalPrimitive();
>
>
>
> }
>
>
>
> TIA,
> Vijay
>
> _______________________________________________
> Web Site - http://www.pjug.org/
> Javamail mailing list
> Javamail at pjug.org
> http://www.pjug.org/mailman/listinfo/javamail
More information about the Javamail
mailing list