๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_scope.asp
Java Scope
In Java, variables are only accessible inside the region where they are created. This is called scope. Variables declared directly inside a method are available anywhere in the method following the line of code in which they were declared:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ variable-scope-in-java
Scope of Variables in Java - GeeksforGeeks
June 15, 2016 - The scope of variables is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e., scope of a variable can be determined at compile time and independent of the ...
People also ask

What are the types of access modifiers in Java?
There are four types of access modifiers in Java: public, protected, default (package) and private. Public make the variable, class or function accessible to anywhere, protected makes it accessible to the class in which it is defined plus itโ€™s sub classes, default (package) makes it accessible to any class in the same package and private makes it accessible to only the class in which it is defined.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ blog โ€บ software development โ€บ scope of a variable in java [with coding example]
Scope of a Variable In Java [With Coding Example]
What is static keyword in Java?
A static keyword in a Java class denotes that the class so declared is not an instance of the class but is a class by itself. The static data members of the class are common to all the instances. A static keyword is used to access static methods and fields of a class. In Java, static keyword is used to access the static methods and fields of a class. To access static methods and fields of a class, you should use the name of the class, a dot (.), and the static method or field name.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ blog โ€บ software development โ€บ scope of a variable in java [with coding example]
Scope of a Variable In Java [With Coding Example]
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ variable scope in java
Variable Scope in Java | Baeldung
January 22, 2019 - 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:
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ variable-scope-in-java
Scope in Java | Codecademy
In Java, scope defines where a certain variable or method is accessible in a program. Variables can be defined as having one of three types of scope:
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java โ€บ java variable scope
Java Variable Scope
September 1, 2008 - All other variables that are not instance and class variables are treated as local variables, including the parameters in a method. The scope of a local variable is within the block in which it is declared, and the lifetime of a local variable ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-scope-of-a-variable-in-java
What is the scope of a variable in Java?
This refers to variables that are defined as static fields of a class. These variables are also accessible within the entire class, including any methods, constructors, and nested classes.
๐ŸŒ
Scientech Easy
scientecheasy.com โ€บ home โ€บ blog โ€บ scope of variables in java
Scope of Variables in Java - Scientech Easy
April 2, 2025 - In other words, the variable scope is the location from which we can access its value. The scope of variables determines its accessibility for other parts of the program. Java allows declaring variables within any block.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ beginners-guide-to-java-scope-cf85302fb79d
Java Scope Explained For Beginners | Medium
February 27, 2024 - Java, one of the most popular ... Java programmer must understand is โ€œscope.โ€ Scope determines the visibility and lifetime of variables and methods within your code....
Find elsewhere
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ blog โ€บ software development โ€บ scope of a variable in java [with coding example]
Scope of a Variable In Java [With Coding Example]
November 25, 2024 - The scope of a variable in ยท Java is static by nature. It means we have to declare it at compile time only.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ scope-of-variables-in-java
Scope of Variables in Java - Javatpoint
Scope of Variables in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
๐ŸŒ
Starter Tutorials
startertutorials.com โ€บ java programming โ€บ core java basics โ€บ scope and lifetime of variables in java
Scope and lifetime of variables in Java with examples - Startertutorials
November 15, 2025 - This article is a part of our core java tutorial. ... Scope of a variable refers to in which areas or sections of a program can the variable be accessed and lifetime of a variable refers to how long the variable stays alive in memory.
๐ŸŒ
NxtWave
ccbp.in โ€บ blog โ€บ articles โ€บ scope-of-variables-in-java
Exploring the Scope of Variables in Java Programming
The scope of variables in Java specifies where the variable can be accessed or modified within the application. It is defined by the place where the variable is declared. The scope specifies which parts of the code may use the variable.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ core java โ€บ java scope
Java Scope
October 11, 2023 - Scope specifically allows you to have different variables with the same name in different parts of the program. Such code is easier to maintain and read. Java scope defines where a certain variable or method is accessible in a program.
๐ŸŒ
OpenJDK
openjdk.org โ€บ jeps โ€บ 429
JEP 429: Scoped Values (Incubator)
The scope of a thing is the space in which it lives โ€” the extent or range in which it can be used. For example, in the Java programming language, the scope of a variable declaration is the space within the program text where it is legal to refer to the variable with a simple name (JLS 6.3).
๐ŸŒ
Java Made Easy!
java-made-easy.com โ€บ variable-scope.html
Variable Scope
In Java, there are some variables that you want to be able to access from anywhere within a Java class. The scope of these variables will need to be at the class level, and there is only one way to create variables at that level โ€“ just inside the class but outside of any methods.
Top answer
1 of 2
11

A local variable1 is "in scope" if code can access it and out of scope if it can't. In Java, variables are scoped to the block ({}) they're declared in. So:

void foo() {
    int a = 42;

    if (/*some condition*/) {
        String q = "Life, the Universe, and Everything";

        // 1. Both `a` and `q` are in scope here
        System.out.println(a);
        System.out.println(q);
        if (/*another condition*/) {
            // 2. Both `a` and `q` are in scope here, too
            System.out.println(a);
            System.out.println(q);
        }
    }

    // 3. Only `a` is in scope here
    System.out.println(a);
    System.out.println(q); // ERROR, `q` is not in scope
}

Note (1), (2), and (3) above:

  1. The code can access q because q is declared in the same block as the code; tt can access a because it's declared in the containing block.

  2. The code can access q because it's declared in the containing block; it can access a because it's in the next block out.

  3. The code can access a, but not q, because q isn't declared in the block or any of the blocks (or a couple of other things) containing it.

When figuring out what an unqualified identifier (like a or q above, as opposed to the foo in this.foo or the toLowerCase in q.toLowerCase, which are qualified) is, the Java compiler will look in each of these places, one after the other, until it finds a match:

  • For a variable with that name in the innermost block
  • For a variable with that name in the next block out, and so on
  • For a field2 or method (generally: member) with that name in the current class
  • For a class with that name from a package that's been imported
  • For a package with that name

There are a few others for that list (I'm not going to get into static imports with a beginner).

There's a lot more to scope, I suggest working through some tutorials and/or a beginning Java book for more.


1 "local variable" vs. "variable" - The Java Language Specification uses "variable" in a more general way than most people do in common speech. When I say "variable" in this answer, I mean what the JLS calls a "local variable".

2 "field" - The JLS calls fields "variables" in some places (and "fields" in other places), hence (1) above. :-)

2 of 2
1

From Section 6.3 of the Java Language Specification:

The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name, provided it is visible.

This concept of scope applies to many kinds of entities in Java: everything from local variables to top-level classes and packages. Even when just talking about variables, there are many cases, from local variables to statically imported fields from another class to the parameter of an exception handler in a catch clause of a try statement. For details, read the JLS or search the web for "Java scope" and read one or more of the many tutorials on the subject that show up.

๐ŸŒ
Masai School
masaischool.com โ€บ blog โ€บ java-variables
Declaration, Scope, and Usage of Java Variables
October 12, 2023 - Consider the following instance of variable declaration: ... The variable's scope pertains to the section of the code wherein the variable can be reached and utilized. Varied levels of visibility are attributed to Java variables, dictating the ...
Top answer
1 of 4
7

According to the JLS, Section 12.4.1, initialization of class variables proceeds from top to bottom, in "textual order":

The static initializers and class variable initializers are executed in textual order, and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope (ยง8.3.2.3). This restriction is designed to detect, at compile time, most circular or otherwise malformed initializations.

So, if you make your own compiler to recognize forward class variable declarations, then it is violating the Java Language Specification.

2 of 4
4

I will give you a simple piece of code:

public class Test
{
    private int foo = bar;
    private int bar = foo;
}

What would you expect this to do?

I presume that designers of Java has done it so because assignment of values to instance variables must be executed in some order. In the case of Java, they are executed downwards (from up to bottom).

EDIT

What about this?

public class Test {
    private int foo = quu++;
    private int bar = quu++;
    private int quu = 1;
}

What values would foo and bar have? Which quu++ statement would be executed first?

My point is, Java designers must have thought that it is counter intuitive to do it as you did describe in your question, i.e. unordered execution with compile time code analysis.

FINAL EDIT

Let's complicate things:

class Test {
    private James james = new James(anInt);
    private Jesse jesse = new Jesse(anInt);
    private IntWrapper anInt = new IntWrapper();
}

class James {

    public James(IntWrapper anInt) {
        if(--anInt.value != 0) {
            new Jesse(anInt);
        }
        else {
            anInt.isJames = true;
        }

    }
}

class Jesse {
    public Jesse(IntWrapper anInt) {
        if(--anInt.value != 0) {
            new James(anInt);
        }
        else {
            anInt.isJames = false;
        }
    }
}

class IntWrapper {
    public int value = 99;
    public boolean isJames;
}

I am not sure what it proves regarding your question because I am not sure about your point.

There is no circular dependency here but value of an instance variable of IntWrapper, isJames, depends on the execution order and it might be difficult to detect this kind of stuff with a lexical/semantic analyzer.