Jump to content

Final Variables and Method Parameters

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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:


public static void f(final int var) {

	System.out.println(var);

}


This method ensures that the user cannot modify var. If the user tries then an error occurs.

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:


public static void f(final int var) {

	System.out.println(var++);

}


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.

It will just say:

Quote

final parameter var may not be assigned

However, it is worth to note that you can do this:


public static void f(final int var) {

     System.out.println(var+1);

}


This isn't modifying the variable.

Final Array

We can declare an array to be final. Let us take a look at what final does with an array.


public static void f(final int[] arr){

        arr[2]++;

}


Call the method with this code:


int[] arr = {3,2,5,4};


f(arr);


Do you think the program will compile, or will it throw a RuntimeException?

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.



public static void f(final int[] arr){

     arr = new int[7];

}


This will throw the RuntimeException.

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:


public final int X = 5;



#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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.

#3
debtboy

debtboy

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 916 posts
Good Tutorial +rep

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Thanks! :)

The great thing is you can make a class final which means that it can't be extended.

final class TheDog {

}

This creates an error:

class TheCat extends TheDog {

}

:) It is very handy in preventing lots of stupid errors. :)

#5
Phineas

Phineas

    Newbie

  • Members
  • PipPip
  • 11 posts
Nice man :P.

#6
LuiDaHottest

LuiDaHottest

    Newbie

  • Members
  • PipPip
  • 20 posts
very nice man

#7
LuiDaHottest

LuiDaHottest

    Newbie

  • Members
  • PipPip
  • 20 posts
cool

#8
Gus

Gus

    Newbie

  • Members
  • Pip
  • 2 posts
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?

#9
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
What do you mean "java is pass by value"? Java Objects are passed by their reference. Only primitives are passed by value.

#10
Gus

Gus

    Newbie

  • Members
  • Pip
  • 2 posts
Hi Sinipull, read this: Does Java pass by reference or pass by value? - JavaWorld

#11
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Yes, but please don't misunderstand it. As he says:

Quote

Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references are copies, swaps will fail.
So no actual copies of the Objects are made, only copies of references.

To ask your question, let's see an example class:
public class Sphere {
 
    public static final double PI = 3.141592653589793;  // this is essentially a constant
 
    public final double radius;
    public final double xpos;
    public final double ypos;
    public final double zpos;
 
    Sphere(double x, double y, double z, double r) {
         radius = r;
         xpos = x;
         ypos = y;
         zpos = z;
    }
 
    [...]
}
As you can see all the fields are final, so when i try to alter them, like that:

        Sphere s = new Sphere(1.00, 1.00, 1.00, 1.00);
        s.ypos = 2; // doesn't compile, because of the "final".
it gives me an error: "The final field Sphere.ypos cannot be assigned"

#12
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
Thanks!!! Expect more from you!!!