Java Basics
Methods 4 - Local variables
- Local variables are declared within a method.
- Local variable lifetime is from method entry to method return.
- Local variable visibility is only within the method.
- Local variables have no initial value.
- Parameter variables are local variables initialized from the argument values.
final
modifier to prevent assignment to parameters.
- Don't assign to parameter variables.
Local variables
Now that we've written two methods, main
and convertKmToMi
,
you should know a little more about the variables in them.
Variables that are declared in a method are called local variables.
They are called local because they can only be referenced and used
locally in the method in which they are declared.
In the method below miles
is a local variable.
private static double convertKmToMi(double kilometers) {
double miles = kilometers * MILES_PER_KILOMETER;
return miles;
}
Visibility: Only in defining method
No code outside a method can see the local variables inside another method. There is no need, or even possibility, of declaring a local variable with a visibility modifier -- local variables are automatically known only in the method itself.
Lifetime: From method call to method return
Local variables are created on the call stack when the method is entered, and destroyed when the method is exited. You can't save values in local variables between calls. For that you have to use instance variables, which you'll learn about a little later.
Initial value: None
Local variables don't have initial values by default -- you can't try to use their value until you assign a value. It's therefore common to assignment a value to them when they're declared.
Compiler error. If you try to use a local variable before it's been assigned a value, the compiler will notice it and give an error message. But the compiler doesn't really know the exact order of execution in a program, so it makes some conservative assumptions. These assumptions can sometimes be too conservative, and there are cases where you must initialize a local variable even though you know it will have a value before it's referenced.
// BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD private static double convertKmToMi(double kilometers) { double miles; return miles; // Won't compile because nothing was assigned to miles. }
Parameters are preinitialized local variables
Method parameters are basically implemented as local variables. They have the same visibility (none outside the method) and lifetime (created on method call, destroyed on method return).
Preinitialized. The difference is that parameters are initialized from the corresponding argument values.
// Both kilometers and miles are implemented as local variables. private static double convertKmToMi(double kilometers) { double miles = kilometers * MILES_PER_KILOMETER; return miles; }
Style: Don't assign to a parameter
You can assign to a parameter variable, just as you would to a local variable, but this is often considered bad style because it can deceive the casual reader in two ways:
- Unexpected meaning change.. Programmers assume parameter variables represent actual argument values. Assigning to parameters breaks that assumption.
- Doesn't change actual argument.
Because formal parameter variables are really local variables,
assigning new values to them doesn't have any effect on the
actual parameters.
However, in some programming languages assignment to a parameter can assign to the corresponding actual parameter (eg, C++ reference parameters). Therefore if you write an assignment to a formal parameter variable, it may mislead the careless programmer with a C++ background. Or the reader may pause and try to decide if you thought you were assigning to the actual argument. In either case it reduces the readability.
Example. The example below shows how a parameter could be reused. The overhead of declaring an extra variable is just about zero, so this really isn't more efficient, and even this small example is astoundingly misleading.
// BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD private static double convertKmToMi(double kilometers) { kilometers = MILES_PER_KILOMETER * kilometers; // BAD - Don't do this, altho it works. return kilometers; }
Style: final
keyword prevents assignment
Some programmers recommend using the final
keyword for each parameter. This prevents assignment to
the parameter. Few programmers do this because
it adds extra clutter, which in a different way reduces
the readability.
The use of self-restraint in
assigning to parameters is usually suffcient, but specifying final
isn't
a bad idea.
private static double convertKmToMi(final double kilometers) {
double miles = kilometers * MILES_PER_KILOMETER;
return miles;
}
Review Questions
[TODO]