The scope of a variable refers to how visible it is to different parts of the program. A variable which is declared inside of a method is said to have local scope - this means that it is visible only within that method. A variable which is declared at the top of a class is said to have class-level scope.
In the example below, a variable called username has been declared twice - each has a different scope. The one at the top has class-level scope and the one in the main method has local scope. The compiler will first attempt to reference the variable locally and if one cannot be found, it will look for one at class-level.
As a result, when the main method references the variable called username, it will be looking at the local declaration and therefore print "KiddThunda". When the variable called username is referenced from methodOne(), "Straven" is printed because it is referencing the variable at class-level.
In the next example, width and height variables are declared and assigned values in the initialiseValues() method.
Because the width and height variables are local to the initialiseValues() method, they cannot be seen from the calculateArea() method - or any other method.
If you require access to a variable(s) from many methods, you can increase the scope of the variable to class-level, as below:
(In this case, because the variables are being called from static methods, the variables are declared with the static keyword - but this is just incidental and not particular to the discussion on scope.)
The scope of a variable has a direct affect on where it can be seen in the program. Additionally, the scope of a variable affects the lifetime of the variable.
Lifetime refers to the period of time that a variable stores a value before having it reset.
Consider the following method which is using the variable numberOfClicks to keep count of the number of clicks. Syntactically correct, however, the logic is incorrect. Each time the method is called, the numberOfClicks variable is being re-declared and set to zero before the variable is incremented. This results in the variable always having a value of 1. We can say that the numberOfClicks variable has a lifetime of 1 method call.
The correct logic is below:
The scope of numberOfClicks has been increased to class-level, therefore extending it's lifetime. Now, the variable is declared and initialised once. When the countClicks() method is called, numberOfClicks is actually incremented. The numberOfClicks variable now has the same lifetime as the object in which it is contained (Scope3Demo).
It is a common scenario that you declare variables locally and then realise that the scope and lifetime of some of them will need to be increased, as your project grows.
Variables may also have block scope - meaning, the are used within the block of a loop or conditional (if) block.
The loop counter in a for loop is a good example of this:
Notice that the loop counter (x) can be accessed within the loop block but not outside of it. We can say that x has a lifetime of the length of the loop.
In the example below, the error is occurring because the variable called message has block scope - that is, the variable has been declared within the conditional block and cannot be seen outside of that block.
We can increase the scope of message if we need to use it beyond the conditional block:




















