+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Final Variables and Method Parameters

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Final Variables and Method Parameters

    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:

    Code:
    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:

    Code:
    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:

    final parameter var may not be assigned
    However, it is worth to note that you can do this:

    Code:
    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.

    Code:
    public static void f(final int[] arr){
            arr[2]++;
    }
    Call the method with this code:

    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.


    Code:
    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:

    Code:
    public final int X = 5;

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Final Variables and Method Parameters

    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.

  4. #3
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Final Variables and Method Parameters

    Good Tutorial +rep

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Final Variables and Method Parameters

    Thanks!

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

    Code:
    final class TheDog {
    
    }
    This creates an error:

    Code:
    class TheCat extends TheDog {
    
    }
    It is very handy in preventing lots of stupid errors.

  6. #5
    Phineas is offline Newbie
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Re: Final Variables and Method Parameters

    Nice man .

  7. #6
    Join Date
    Jan 2010
    Posts
    18
    Rep Power
    0

    Re: Final Variables and Method Parameters

    very nice man

  8. #7
    Join Date
    Jan 2010
    Posts
    18
    Rep Power
    0

    Re: Final Variables and Method Parameters

    cool

  9. #8
    Gus
    Gus is offline Newbie
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Re: Final Variables and Method Parameters

    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?

  10. #9
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: Final Variables and Method Parameters

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

  11. #10
    Gus
    Gus is offline Newbie
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Re: Final Variables and Method Parameters


+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. Replies: 4
    Last Post: 09-02-2011, 06:03 PM
  2. Using an inherited method in another class' method
    By ZipOnTrousers in forum Java Help
    Replies: 1
    Last Post: 11-13-2009, 09:39 AM
  3. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 2
    Last Post: 09-19-2009, 06:18 AM
  4. Final Project
    By TcM in forum General Programming
    Replies: 24
    Last Post: 10-07-2008, 12:46 PM
  5. Final Fantasy III
    By littlefranciscan in forum Video Game Talk
    Replies: 1
    Last Post: 02-25-2007, 11:52 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts