Most developers (certainly myself!) are lazy. We look to re-use code and write the briefest code we can.

Sometimes it backfires!

When writing a method, either in Java or LotusScript, we can often pass parameters around with little concern. I came across that some years ago with LotusScript when objects were being changed by a function when I didn’t intend that. That was when I learned that you could define a sub or function in two ways. So consider the following code:

Dim myStr as String
myStr = "Test"
Call mySub(myStr)
print myStr

Sub mySub(passedVal as String)
passedVal = "Test 2"
End Sub

What gets printed is actually “Test 2”, because myStr is passed in by reference rather than by value. So you’re passing in the String object, not passing its value to the sub / function. passedVal is the same object as myStr, so when you change one, you change both. The alternative would have been to enter:

Sub mySub (passedVal byVal as String)

Then what gets printed is “Test”, because you’re copying the value into a new String object called passedVal.

In Java, you use the keyword “final” to ensure an object passed in as a parameter cannot be changed. So:

readValue(final ViewEntry ve, final Vector<Object> columnValues)

It’s best practice to do so, though there may be times when it is more convenient to allow a method to modify values. There is a way to automatically add final to your parameters, in the Java Editor save actions preference, as Per Henrik Lausten pointed out. But note that may change references you don’t want it to.

 

5 thoughts on “Method Parameters and The Importance of Final in Java”

  1. Sorry to say, but all you know about final is completly wrong. First in Java the String class ist immutable. So you cannot change the value of a String object. So your problem in lotus script can not happen in Java. And your second big mistake is that you think that the final modifier makes an object immutable. That is not the case. In your example you can modify the Vector Object without a restriction. You cann add or remove items.

    So what does final do? final tells the compiler that this reference variable points to a specific object and that this reference cannot be changed. So to be absolutly clear final prevents you from changing the pointer of an instance variable to another object. It does not prevent you from changing the object.

  2. In your example:

    readValue(final ViewEntry ve, final Vector columnValues)

    the final directive does not help much. The columnValues Vector can still be changed internally by calling Vector methods like .add(…), .remove(…).

    That’s what Collections.unmodifiableList() is meant for, which wraps a Java List and throws an exception when somebody wants to change the list content.

    And the LotusScript code

    Sub mySub(passedVal as String)
    passedVal = “Test 2”
    End Sub

    is not possible in Java with and without the final directive. You don’t change the original reference, just the reference locally available in the method code.

    Using final in method arguments might be considered best practice because the code is more readable, e.g. you can see the original method arguments in the debugger all the time.

  3. Unsurprisingly, I second Karsten’s entire comment. At a technical level, _all_ parameters in Java are by value – it’s just that the value of non-primitives is the pointer to the object, so you can still modify the contents of the object itself, but reassigning the variable points it to another object.

    He also has an important distinction between “final” and “immutable”. If you declare a primitive, it is also immutable – “final int foo = 3” can never be anything other than 3. However, “final List foo = new ArrayList()” says nothing about the contents of that ArrayList, just that “foo” can never point to a different list.

    The value remains, though: it’s definitely good to keep your method params final just for clarity and discipline.

  4. Seems like I confused myself (seems to be the week for that!). Maybe I masked the solution by adding final to the object I was passing to a Java method and at the same time writing its values into another object.

    But this does highlight further the benefits of blogging, to clarify your own misunderstandings and learn! Thanks for all the comments.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.