Modify the scope of a variable

Completed

The scope of a variable defines where the variable can be accessed. Variables that are declared in the class declarations are called instance variables. Instance variables, if they aren't declared private, can be accessed from any method in the class and from methods within a class that are extended from the original class.

Local variables are declared in a method and can only be accessed in the method that they are declared in. When a variable is declared, it takes up memory to store the value.

Declaring all your variables in the class declaration allocates memory for every variable until the process is finished. Using local variables can help reduce overhead by only allocating memory during the time when the method is running. When the code block in which the variable is declared finishes, the memory is freed. We recommend that you declare variables at the smallest scope possible to optimize memory efficiency.