Phoenix
Phoenix

Reputation: 8933

Initialization of local variables

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);
    }
}

Upvotes: 0

Views: 93

Answers (5)

Peter Walser
Peter Walser

Reputation: 15706

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.

Upvotes: 0

Lee Meador
Lee Meador

Reputation: 12985

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 ...
   }

Upvotes: 0

mayabelle
mayabelle

Reputation: 10014

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

Upvotes: 1

Alowaniak
Alowaniak

Reputation: 600

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.

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280179

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.

Upvotes: 2

Related Questions