---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 17 Edition"
java_version: 17
chapter: "Chapter 6. Names"
section: "6.3"
heading: "6.3. Scope of a Declaration"
parent: "Chapter 6. Names"
source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-6.html#jls-6.3
id: "6.3"
url: https://jvm.obfuscated.software/jls/se17/6.3.html
raw: https://jvm.obfuscated.software/jls/se17/6.3.md
---

# 6.3. Scope of a Declaration

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 not shadowed ([§6.4.1](6.4.1.md)).

A declaration is said to be *in scope* at a particular point in a program if and only if the declaration's scope includes that point.

The scope of the declaration of an observable top level package ([§7.4.3](7.4.3.md)) is all observable compilation units associated with modules to which the package is uniquely visible ([§7.4.3](7.4.3.md)).

The declaration of a package that is not observable is never in scope.

The declaration of a subpackage is never in scope.

The package `java` is always in scope.

The scope of a class or interface imported by a single-type-import declaration ([§7.5.1](7.5.1.md)) or a type-import-on-demand declaration ([§7.5.2](7.5.2.md)) is the module declaration ([§7.7](7.7.md)) and all the class and interface declarations ([§8.1](8.1.md), [§9.1](9.1.md)) of the compilation unit in which the `import` declaration appears, as well as any annotations on the module declaration or package declaration of the compilation unit.

The scope of a member imported by a single-static-import declaration ([§7.5.3](7.5.3.md)) or a static-import-on-demand declaration ([§7.5.4](7.5.4.md)) is the module declaration and all the class and interface declarations of the compilation unit in which the `import` declaration appears, as well as any annotations on the module declaration or package declaration of the compilation unit.

The scope of a top level class or interface ([§7.6](7.6.md)) is all class and interface declarations in the package in which the top level class or interface is declared.

The scope of a declaration of a member `m` declared in or inherited by a class or interface C ([§8.2](8.2.md), [§9.2](9.2.md)) is the entire body of C, including any nested class or interface declarations.

The scope of a formal parameter of a method ([§8.4.1](8.4.1.md)), constructor ([§8.8.1](8.8.1.md)), or lambda expression ([§15.27](15.27.md)) is the entire body of the method, constructor, or lambda expression.

The scope of a class's type parameter ([§8.1.2](8.1.2.md)) is the type parameter section of the class declaration, and the type parameter section of any superclass type or superinterface type of the class declaration, and the class body. If the class is a record class ([§8.10](8.10.md)), then the scope of the type parameter additionally includes the header of the record declaration ([§8.10.1](8.10.1.md)).

The scope of an interface's type parameter ([§9.1.2](9.1.2.md)) is the type parameter section of the interface declaration, and the type parameter section of any superinterface type of the interface declaration, and the interface body.

The scope of a method's type parameter ([§8.4.4](8.4.4.md)) is the entire declaration of the method, including the type parameter section, but excluding the method modifiers.

The scope of a constructor's type parameter ([§8.8.4](8.8.4.md)) is the entire declaration of the constructor, including the type parameter section, but excluding the constructor modifiers.

The scope of a local class or interface declaration immediately enclosed by a block ([§14.2](14.2.md)) is the rest of the immediately enclosing block, including the local class or interface declaration itself.

The scope of a local class or interface declaration immediately enclosed by a switch block statement group ([§14.11](14.11.md)) is the rest of the immediately enclosing switch block statement group, including the local class or interface declaration itself.

The scope of a local variable declared in a block by a local variable declaration statement ([§14.4.2](14.4.2.md)) is the rest of the block, starting with the declaration's own initializer and including any further declarators to the right in the local variable declaration statement.

The scope of a local variable declared in the *ForInit* part of a basic `for` statement ([§14.14.1](14.14.1.md)) includes all of the following:

- Its own initializer
- Any further declarators to the right in the *ForInit* part of the `for` statement
- The *Expression* and *ForUpdate* parts of the `for` statement
- The contained *Statement*

The scope of a local variable declared in the header of an enhanced `for` statement ([§14.14.2](14.14.2.md)) is the contained *Statement*.

The scope of a local variable declared in the resource specification of a `try`-with-resources statement ([§14.20.3](14.20.3.md)) is from the declaration rightward over the remainder of the resource specification and the entire `try` block associated with the `try`-with-resources statement.

The translation of a `try`-with-resources statement implies the rule above.

The scope of a parameter of an exception handler that is declared in a `catch` clause of a `try` statement ([§14.20](14.20.md)) is the entire block associated with the `catch`.

**Example 6.3-1. Scope of Class Declarations**

These rules imply that declarations of class and interface types need not appear before uses of the types. In the following program, the use of `PointList` in class `Point` is valid, because the scope of the class declaration `PointList` includes both class `Point` and class `PointList`, as well as any other class or interface declarations in other compilation units of package `points`.

```
package points;
class Point {
    int x, y;
    PointList list;
    Point next;
}

class PointList {
    Point first;
}
```

**Example 6.3-2. Scope of Local Variable Declarations**

The following program causes a compile-time error because the initialization of local variable `x` is within the scope of the declaration of local variable `x`, but the local variable `x` does not yet have a value and cannot be used. The field `x` has a value of `0` (assigned when `Test1` was initialized) but is a red herring since it is shadowed ([§6.4.1](6.4.1.md)) by the local variable `x`.

```
class Test1 {
    static int x;
    public static void main(String[] args) {
        int x = x;
    }
}
```

The following program does compile:

```
class Test2 {
    static int x;
    public static void main(String[] args) {
        int x = (x=2)*2;
        System.out.println(x);
    }
}
```

because the local variable `x` is definitely assigned ([§16 (*Definite Assignment*)](16.md)) before it is used. It prints:

```
4
```

In the following program, the initializer for `three` can correctly refer to the variable `two` declared in an earlier declarator, and the method invocation in the next line can correctly refer to the variable `three` declared earlier in the block.

```
class Test3 {
    public static void main(String[] args) {
        System.out.print("2+1=");
        int two = 2, three = two + 1;
        System.out.println(three);
    }
}
```

This program produces the output:

```
2+1=3
```

The scope of a pattern variable declaration (that is, a local variable declared by a pattern) is the part of the program that might be executed after the matching of a value against the pattern has succeeded ([§14.30.2](14.30.2.md)). It is determined by considering the program points where the pattern variable is *definitely matched* in a region beginning with the pattern that declares the pattern variable.

The remainder of this section is devoted to a precise explanation of the words "definitely matched". The analysis takes into account the structure of statements and expressions, with a special treatment for the boolean expression operators and certain statement forms.

It will be seen that the scope of a pattern variable declaration is a flow-dependent concept similar to definite assignment ([§16 (*Definite Assignment*)](16.md)). The rules defined in the rest of this section deliberately have a similar form to the rules of definite assignment.

The analysis relies on the technical term "introduced by", which has the following form:

- a pattern variable is *introduced by* an expression *when true*
- a pattern variable is *introduced by* an expression *when false*
- a pattern variable is *introduced by* a statement

The simplest example is that the pattern variable `s` is introduced by the expression `a instanceof String s` when true. In other words, if the value of the expression is `true` then the pattern matching must have succeeded, and thus the pattern variable must have been assigned a value.

In contrast, the pattern variable `t` is introduced by the expression `!(b instanceof Integer t)` when false. This is because the pattern matching could only have succeeded if the value of the expression is `false`.

**Sections:**

- [6.3.1. Scope for Pattern Variables in Expressions](6.3.1.md)
- [6.3.2. Scope for Pattern Variables in Statements](6.3.2.md)
