This is because object
is not final
in your second method.
So:
public String getLogIdentifierFromFile(final File file) {
//Have a final reference to file
//Pass it into nullify
nullify(file);
//Reference to file is unchanged
return "";
}
And:
public void nullify(Object object) {
//get a reference to some object
//set it to null
object = null;
}
Think of this as getLogIdentifierFromFile
is passing a business card with the details of file
to nullify
and then nullify
is scratching off the address on the card.
This does not affect where file
lives, nullify
just forgets where file
lives.
Java is slightly confusing in this regard as it passes object references by value. This means that when you pass on your reference to another method it create a copy of the reference and passes that copy.
This means that if you carry out actions on the reference (such as File.setExecutable()
) the actions will happen on the referenced object. If you change the reference itself, i.e. reassign it, then this only affects the local copy.
final
only prevents the reassignment of a reference.