Final Variables and Method Parameters
A parameter to a function can be declared with the keyword final. This indicates that the parameter cannot be modified in the function. The final keyword can allow you to make a variable a constant.
You might want to pass an array but ensure that the method doesn't modify the array.
Example of using final:
This method ensures that the user cannot modify var. If the user tries then an error occurs.Code:public static void f(final int var) { System.out.println(var); }
You can call this method just like any other method. The key here is that the var variable cannot be modified.
Let us try to modify the variable.
Change the function to:
If I try to compile this code and call this method then a RuntimeException is thrown. However, this might not be easy to find. You may accidentally modify the variable in the method without realizing it. The nice thing is Netbeans won't compile my program with this error.Code:public static void f(final int var) { System.out.println(var++); }
It will just say:
However, it is worth to note that you can do this:final parameter var may not be assigned
This isn't modifying the variable.Code:public static void f(final int var) { System.out.println(var+1); }
Final Array
We can declare an array to be final. Let us take a look at what final does with an array.
Call the method with this code:Code:public static void f(final int[] arr){ arr[2]++; }
Do you think the program will compile, or will it throw a RuntimeException?Code:int[] arr = {3,2,5,4}; f(arr);
It actually will compile. The program isn't modifying the array. It is modifying an item in the array. However, trying to allocate a new array will throw an error.
E.g.
This will throw the RuntimeException.Code:public static void f(final int[] arr){ arr = new int[7]; }
The final keyword is great if you want to ensure that you do not modify a variable when you pass it to a function. You can also use it with class variables. This is similar to using const in C++.
Final Variables
Similarly, a variable can be declared final. You can also declare classes as final (meaning that they cannot be extended).
Example:
Code:public final int X = 5;
That is a handy keyword. I never knew final existed for Java (which is no surprise since I am not a Java Programmer). Nicely done, +rep.
Thanks!
The great thing is you can make a class final which means that it can't be extended.
This creates an error:Code:final class TheDog { }
Code:class TheCat extends TheDog { }It is very handy in preventing lots of stupid errors.
![]()
Nice man.
very nice man
cool
since java is pass by value, why do we need final for the parameter? just to make sure that the parameter is not modified locally to the method?
What do you mean "java is pass by value"? Java Objects are passed by their reference. Only primitives are passed by value.
Hi Sinipull, read this: Does Java pass by reference or pass by value? - JavaWorld
There are currently 2 users browsing this thread. (0 members and 2 guests)
Bookmarks