---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 25 Edition"
java_version: 25
chapter: "Chapter 4. Types, Values, and Variables"
section: "4.12.3"
heading: "4.12.3. Kinds of Variables"
parent: "4.12. Variables"
source: https://docs.oracle.com/javase/specs/jls/se25/html/jls-4.html#jls-4.12.3
id: "4.12.3"
url: https://jvm.obfuscated.software/jls/se25/4.12.3.html
raw: https://jvm.obfuscated.software/jls/se25/4.12.3.md
---

# 4.12.3. Kinds of Variables

There are eight kinds of variables:

1. A *class variable* is a field declared using the keyword `static` within a class declaration ([§8.3.1.1](8.3.1.1.md)), or with or without the keyword `static` within an interface declaration ([§9.3](9.3.md)).

   A class variable is created when its class or interface is prepared ([§12.3.2](12.3.2.md)) and is initialized to a default value ([§4.12.5](4.12.5.md)). The class variable effectively ceases to exist when its class or interface is unloaded ([§12.7](12.7.md)).

2. An *instance variable* is a field declared within a class declaration without using the keyword `static` ([§8.3.1.1](8.3.1.1.md)).

   If a class T has a field `a` that is an instance variable, then a new instance variable `a` is created and initialized to a default value ([§4.12.5](4.12.5.md)) as part of each newly created object of class T or of any class that is a subclass of T ([§8.1.4](8.1.4.md)). The instance variable effectively ceases to exist when the object of which it is a field is no longer referenced, after any necessary finalization of the object ([§12.6](12.6.md)) has been completed.

3. *Array components* are unnamed variables that are created and initialized to default values ([§4.12.5](4.12.5.md)) whenever a new object that is an array is created ([§10 (*Arrays*)](10.md), [§15.10.2](15.10.2.md)). The array components effectively cease to exist when the array is no longer referenced.

4. *Method parameters* ([§8.4.1](8.4.1.md)) name argument values passed to a method.

   For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked ([§15.12](15.12.md)). The new variable is initialized with the corresponding argument value from the method invocation. The method parameter effectively ceases to exist when the execution of the body of the method is complete.

5. *Constructor parameters* ([§8.8.1](8.8.1.md)) name argument values passed to a constructor.

   For every parameter declared in a constructor declaration, a new parameter variable is created each time a class instance creation expression ([§15.9](15.9.md)) or constructor invocation ([§8.8.7](8.8.7.md)) invokes that constructor. The new variable is initialized with the corresponding argument value from the creation expression or constructor invocation. The constructor parameter effectively ceases to exist when the execution of the body of the constructor is complete.

6. *Lambda parameters* ([§15.27.1](15.27.1.md)) name argument values passed to a lambda expression body ([§15.27.2](15.27.2.md)).

   For every parameter declared in a lambda expression, a new parameter variable is created each time a method implemented by the lambda body is invoked ([§15.12](15.12.md)). The new variable is initialized with the corresponding argument value from the method invocation. The lambda parameter effectively ceases to exist when the execution of the lambda expression body is complete.

7. An *exception parameter* is created each time an exception is caught by a `catch` clause of a `try` statement ([§14.20](14.20.md)).

   The new variable is initialized with the actual object associated with the exception ([§11.3](11.3.md), [§14.18](14.18.md)). The exception parameter effectively ceases to exist when execution of the block associated with the `catch` clause is complete.

8. *Local variables* ([§14.4](14.4.md)) are declared by statements ([§14.4.2](14.4.2.md), [§14.14.1](14.14.1.md), [§14.14.2](14.14.2.md), [§14.20.3](14.20.3.md)) and by patterns ([§14.30](14.30.md)). A local variable declared by a pattern is called a *pattern variable*.

   A local variable declared by a statement is created when the flow of control enters the nearest enclosing block ([§14.2](14.2.md)), `for` statement, or `try`-with-resources statement.

   A local variable declared by a statement is initialized as part of the execution of the statement, provided the variable's declarator has an initializer. The rules of definite assignment ([§16 (*Definite Assignment*)](16.md)) prevent the value of a local variable declared by a statement from being used before it has been initialized or otherwise assigned a value.

   A local variable declared by a pattern is created and initialized when the pattern matches ([§14.30.2](14.30.2.md)). The rules of scoping ([§6.3](6.3.md)) prevent the value of a local variable declared by a pattern from being used unless the pattern has matched.

   A local variable ceases to exist when its declaration is no longer in scope.

   Were it not for one exceptional situation, a local variable declared by a statement could always be regarded as being created when the statement is executed. The exceptional situation involves the `switch` statement ([§14.11](14.11.md)), where it is possible for control to enter a block but bypass execution of a local variable declaration statement. Because of the restrictions imposed by the rules of definite assignment ([§16 (*Definite Assignment*)](16.md)), however, the local variable declared by such a bypassed local variable declaration statement cannot be used before it has been definitely assigned a value by an assignment expression ([§15.26](15.26.md)).

**Example 4.12.3-1. Different Kinds of Variables**

```
class Point {
    static int numPoints;   // numPoints is a class variable
    int x, y;               // x and y are instance variables
    int[] w = new int[10];  // w[0] is an array component
    int setX(int x) {       // x is a method parameter
        int oldx = this.x;  // oldx is a local variable
        this.x = x;
        return oldx;
    }
    boolean equalAtX(Object o) {
        if (o instanceof Point p)  // p is a pattern variable
            return this.x == p.x;
        else
            return false;
    }
}
```
