Variable Scope in Java
Variable scope in Java refers to where a variable is accessible. With scope, we can reuse variables with the same name as long as they are part of different scopes. In this post, we'll look at the different ways scope is written: class, method, and block scopes.
Concept
Typically, the use of curly brackets define a scope. Nested curly brackets can access variables in the parent scope, but not child scopes like so:
{ // Root ScopeInteger scope = 0;// Can only access scope{Integer innerScope = 1;// Can access innerScope and scope{Integer innerInnerScope = 2;// Can access innerInnerScope, innerScope, and scope}}}
Class Scope
When writing classes, variables can be declared as properties of the class. Here, we declare the variable number
. All methods inside of the Example
class can access the number
variable due to class scope.
public class Example {public Integer number = 0;public void method1() {System.out.println(number);}public void method2() {System.out.println(number);}}
Method Scope
Variables can be declared inside methods. In this scenario, the variable number
is declared within method1
. This means that this variable can be accessed only within the method1
, and not within method2
.
public class Example {public void method1() {Integer number = 0;System.out.println(number);}public void method2() {// Does not have access to the variable number in method1}}
Block Scope
Block scopes are used within methods. For example if statement and for loops define a block scope.
public class Example {public void method() {Integer number = 0;if (number >= 0) {Integer anotherNumber = 1;System.out.println(anotherNumber);}for (int i = 0; i < 10; i++) {Integer anotherNumber = 2;System.out.println(anotherNumber);}// Does not have access to the variable anotherNumber}}
With nested block scopes, child scopes can access variables in parent scopes:
public class Example {public void method() {Integer number = 0;if (number >= 0) {Integer anotherNumber = 1;System.out.println(anotherNumber);if (number == 0) {Integer anotherNumberNested = 2;System.out.println(anotherNumberNested);System.out.println(anotherNumber);}// Does not have access to the variable anotherNumberNested}// Does not have access to the variable anotherNumber}}
Since variables are only available within a certain scope, we can declare the variable with the same name if we are outside the scope:
public class Example {public void method() {Integer number = 0;if (number >= 0) {Integer anotherNumber = 1;System.out.println(anotherNumber);if (number == 0) {Integer anotherNumberNested = 2;System.out.println(anotherNumberNested);System.out.println(anotherNumber);}// Able to declare variable name anotherNumberNestedInteger anotherNumberNested = 3;System.out.println(anotherNumberNested);}// Able to declare variable name anotherNumberInteger anotherNumber = 4;System.out.println(anotherNumber);}}
Conclusion
Here we covered variable scope in Java and how they change the way variables can be accessed. Child scopes can access variables in its parent scopes and variable names can be reused if they are not part of the same scope.