Tuesday, February 5, 2013

Variable Scope: Local Variables




Local variables are the medium to small scope variables. Usually, you will use these much more frequently than instance variables.
Local variables are declared inside of a method. Their life is only as long as the method is running and then they're gone. This means that two different methods can have the same variables inside of them.


However, since the variable's scope is only the method it's declared in, they can't be used in a different method if they are not declared.
Compiler error using variables out of scope

Why should you care?

As with instance variables, local variable scope can often be figured out by trial and error. However, understanding how local variables work can save you a lot of time as a programmer.


No comments:

Post a Comment