Question Regarding Final keyword

hi all,

First of all sorry for my lack of knowldge, i was seeig a sample code on web and i see following line

public Movie findMovieById(final String id)

in above line do some explain why we using final keyword according to me its used for constants.

wat is the difference between

public Movie findMovieById(final String id)

and

public Movie findMovieById(String id)

and what advantages we will get from it.

regards

[477 byte] By [emmi@javaa] at [2007-11-26 19:54:21]
# 1
The String id is used in an inner class defined in the findMovieById method, or, basically an object representing an ID can't be reassigned different value.
hiwaa at 2007-7-9 22:46:45 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi,

Within the function public Movie findMovieById(String id)

, you can write id = "new_movie_id";

i.e. within the method, you can assign the reference "id" to any other string.

However, if you attempt to assign a String to the "id" reference within public Movie findMovieById(final String id)

, you get a compilation error.

If a formal argument to a method is declared as final, it means that the argument is never re-assigned to another object within the method. Suppose the method "findMovieById" is declared with the "id" argument as "final", we have:

String id = "ABCD1234";

findMovieById(id);

System.out.println(id); //Always will print "ABCD1234"

The advantage of qualifying a formal method parameter as "final" is that the caller can be sure that the method parameter will never change its reference even after the method is executed.

Regards,

Kumar.

kumar_iyera at 2007-7-9 22:46:45 > top of Java-index,Java Essentials,Java Programming...
# 3

> The advantage of qualifying a formal method parameter

> as "final" is that the caller can be sure that the

> method parameter will never change its reference even

> after the method is executed.

The caller really couldn't care less. The argument being final or not is absolutely moot from a caller's point of view. Please read up about pass-by-value - there is nothing you can do to make your example code print anything else even if the variable were non-final.

the reason to declare the argument as final is soleley conceptual, there is no technical necessity. It says "this is the stuff you're given and you are supposed to work with - if you are going to reassign the value, than you are likely do something conceptually wrong and potentially confusing to the code reader, thus I won't allow it".

CeciNEstPasUnProgrammeura at 2007-7-9 22:46:45 > top of Java-index,Java Essentials,Java Programming...
# 4

> String id = "ABCD1234";

> findMovieById(id);

> System.out.println(id); //Always will print "ABCD1234"

Counter-proof, just to show how wrong you are generally seen:

StringBuffer id = new StringBuffer("ABCD1234");

findMovieById(id);

System.out.println(id); // Borked.

findMovieById(final StringBuffer id) {

id.append("whatever");

}

CeciNEstPasUnProgrammeura at 2007-7-9 22:46:45 > top of Java-index,Java Essentials,Java Programming...
# 5
if you make the argument as final you will not be able to change the reference but you can change the state. In your example you have used a string which is immutable Because of this you wont be able to change the value either.
Basenta at 2007-7-9 22:46:45 > top of Java-index,Java Essentials,Java Programming...
# 6

Hi,

I had quoted the example keeping in mind that "String" objects are immutable. The point I wanted to drive home is that a formal argument reference declared "final" cannot be made to refer to anything else, within the method, i.e.

id = new StringBuffer("ABCD1234whatever");

would be illegal within the method if it declares "id" as final.

Regards,

Kumar.

kumar_iyera at 2007-7-9 22:46:45 > top of Java-index,Java Essentials,Java Programming...