1. Overview
In Java, as in any programming language, each variable has a scope. This is the segment of the program where a variable can be used and is valid.
In this tutorial, we’ll introduce the available scopes in Java and discuss the differences between them.
2. Class Scope
Each variable declared inside of a class’s brackets ( {} ) but outside of any method, has class scope. As a result, these variables can be used everywhere in the class, but not outside of it:
public class ClassScopeExample { Integer amount = 0; public void exampleMethod() { amount++; } public void anotherExampleMethod() { Integer anotherAmount = amount + 4; } }
We can see that ClassScopeExample has a class variable amount that can be accessed within the class’s methods.
3. Method Scope
When a variable is declared inside a method, it has method scope and it will only be valid inside the same method:
public class MethodScopeExample { public void methodA() { Integer area = 2; } public void methodB() { // compiler error, area cannot be resolved to a variable area = area + 2; } }
In methodA, we created a method variable called area. For that reason, we can use area inside methodA, but we can’t use it anywhere else.
4. Loop Scope
If we declare a variable inside a loop, it will have a loop scope and will only be available inside the loop:
public class LoopScopeExample { List<String> listOfNames = Arrays.asList("Joe", "Susan", "Pattrick"); public void iterationOfNames() { String allNames = ""; for (String name : listOfNames) { allNames = allNames + " " + name; } // compiler error, name cannot be resolved to a variable String lastNameUsed = name; } }
We can see that method iterationOfNames has a method variable called name. This variable can be used only inside the loop and is not valid outside of it.
5. Bracket Scope
We can define additional scopes anywhere using brackets ({}):
public class BracketScopeExample { public void mathOperationExample() { Integer sum = 0; { Integer number = 2; sum = sum + number; } // compiler error, number cannot be solved as a variable number++; } }
The variable number is only valid within the brackets.
6. Scopes and Variable Shadowing
Imagine that we have a class variable, and we want to declare a method variable with the same name:
public class NestedScopesExample { String title = "Baeldung"; public void printTitle() { System.out.println(title); String title = "John Doe"; System.out.println(title); } }
The first time that we are printing title, it will print “Baeldung”. After that, declare a method variable with the same name and assign to it the value “John Doe“.
The title method variable overrides the possibility to access to the class variable title again. That’s why the second time, it will print “John Doe“.
Confusing, right? This is called variable shadowing and isn’t a good practice. It’s better to use the prefix this in order to access the title class variable, like this.title.
7. Conclusion
We learned the different scopes that exist in Java. Every variable’s scope is limited to the brackets that contain it and starts in the line that it’s declared.
As always, the code is available over on GitHub.