Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


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 ( {} ) with private access modifier 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 {
    private Integer amount = 0;
    public void exampleMethod() {
    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.

If we don't use private, it will be accessible from the entire package. Check access modifiers article for additional information.

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

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() {
        String title = "John Doe";

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.

As always, the code is available on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


Generic footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!