0

What's the difference between the declaring an uninitialized final variable and setting a final variable to null?

void A(String pizza) {
    String retVal = null;

    if (StringUtils.isBlank(pizza)) {
       retVal = "blank"
    } else {
       retVal = computeString(pizza);
    }
}

void A(String pizza) {
    final String retVal;

    if(StringUtils.isBlank(pizza)) {
       retVal = "blank"
    } else {
       retVal = computeString(pizza);
    }
}
1
  • 1
    Did you mean to have a final on the first A? Commented Oct 11, 2013 at 17:58

5 Answers 5

2

Maybe I didn't understand, but in your second example, you won't be able to reassign retVal after your if-else block. A final variable

may only be assigned to once. Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.

If you had set your final variable to null, you would not be able to reassign it in the if block.

Sign up to request clarification or add additional context in comments.

Comments

1

If you set a final variable to null you'll never be able to assign anything else to it... A final variable (itself) can never change.

Comments

1

The difference is that a final variable can never be changed to have another value.

Comments

0

In this one, the retVal = null accomplishes nothing. You give it a value of null. You never have code that uses that value. Then you give it another value, depending on whether you do the if-then or the else part.

In code that falls where I've added the comment, you can use or change the value of retVal.

  void A(String pizza) {
      String retVal = null;
      ... code in here could use it or give it a new value ...
      if(StringUtils.isBlank(pizza) {
         retVal = "blank"
      } else {
         retVal = computeString(pizza);
      }
      ... additional code here might change it (and can use it's value) ...
   }

In this one, you are required to give retVal a value everytime the method is called. Your if-then-else code does that. The value can never be changed after it is given a value.

One difference is that the compiler would tell you if you used retVal before giving it a value. It would, reasonably, tell you that the variable has no value yet.

   void A(String pizza) {
      final String retVal;
      ... code in here cannot use it or give it a value, too ...
      if(StringUtils.isBlank(pizza) {
         retVal = "blank"
      } else {
         retVal = computeString(pizza);
      }
      ... additional code here cannot change it but can use it's value ...
   }

Comments

0

final means:

  • You have to assign something (even if it's null)
  • You can't change the reference to anything else afterwards (but you can, of course, modify the referenced object).

The meaning is highly semantic, and makes sure you won't accidentally forget to care about what to assign, and you can code with the guarantee that the value is not accidentally changed.
Omitting this modifier just removes that guarantee, nothing else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.