What are the different types of variable in Java ?

What are the different types of variable in Java ? Some websites say 3, others 4 – generally combinations of static variables, instance variables, method parameters and local variables.

Let’s see what the Java Language Specification has to say about it.

The 8 types of variable in Java

The JLS makes an exhaustive list of 8 possible variable types : class variables, instance variables, array components, method parameters, constructor parameters, lambda parameters, exception parameters, local variables.

  1. Class variables. A class variable is :
  • Any field declared using the keyword static in a class
  • Or any field declared in an interface (the keyword static is redundant for variables defined in interfaces).
class Man {
    static String sex; // class variable
}
interface Person {
    static int numberOfArms = 2; // class variable - even though we're in an interface
}

Class variables are sometimes referred to as static variables.

2. Instance variables

An instance variable is a field declared within a class declaration without using the static keyword. An instance variable is created every time a new instance of the class in which it is contained is created, or any class that is a subclass of the original class.

class Man {
String name; // instance variable
}

3. Array components

Array components are unnamed variables that are created whenever a new object that is an array is created. This is generally not very useful to know in real life, but there may be cases where it’s good to know that declaring an array of size 10000 also creates 10000 variables.

int[] myArray = new int[10]; // 10 array components created
int[0]; // access an array component

4. Method parameters

Method parameters name argument values passed to a method. They’re created each time that method is invoked – this means that a variable with the name provided in the method signature is created.

void repeat(String message) { // method parameter 
System.out.println(message);
}

5. Constructor parameters

Constructor parameters work much the same way as method parameters. They’re created each time a constructor is invoked, either through class instance creation expression (new MyClass();) or explicit constructor invocation (this(); or super(); in a constructor body).

class Person {   
public Person(String name) { // constructor parameter   
} }

6. Lambda parameters

Lambda parameters name argument values passed to a lambda expression body. They’re created when the lambda body is invoked.

List<String> flavours = Arrays.asList("Chocolate", "Vanilla", "Caramel");
List<String> filtered = flavours.stream().filter(flav -> flav.contains("V")).toList(); // flav is a lambda parameter

7. Exception parameters

An exception parameter is created each time an exception is caught by a catch clause of a try statement. The new variable is initialized with the object associated with the exception.

try {
...
} catch (Exception ex) { // ex is an exception parameter
}

8. Local variables

Local variables are declared by statements and by patterns. A local variable declared by a statement is created when the flow of control enters the nearest enclosing block, for statement, or try-with-resources statement.

void myMethod() {
int i = 1; // local variable
}

A local variable declared by a pattern is called a pattern variable. A pattern variable is created and initialized when (and only when) the pattern matches. These were introduced in Java 16.

if (myObj instanceof String myObjAsString) { //myObjAsString is a pattern variable

What are the default values for each type of variable ?

  1. Class variables, instance variables, and array components are initialized to their default values. The default values are :
  • For type byte, short, int, long, the default value is 0.
  • For type float and double, the default value is also 0.
  • For type char, the default value is the null character, that is, ‘\u0000’.
  • For type boolean, the default value is false.
  • For all other types, ie class instances, the default value is null.

2. Method parameters and constructor parameters are initialized to the value passed by the invoker of the method or constructor. Unlike other languages like Swift, method and constructor parameters cannot have a default value.

3. Lambda parameters have the value of whatever object (in the stream) the lambda body is operating on at that given moment. There’s not much to say and not much to know on this: basically there are no default values for lambda parameters, and it’s not like you can predict the order in which the items in the stream will be consumed by the lambda, either.

4. Exception parameters are initialized with the instance of the caught exception’s class.

5. Pattern variables are initialized implicitly by the process of pattern matching. If and only if the pattern is matched, then the pattern variable takes the value of the object on the left-hand side of the pattern match.

Other local variables have no default value, and must explicitly be given one before the value is used.

Read more on the Java Language Specification, section 4.12.3 : https://docs.oracle.com/javase/specs/jls/se19/jls19.pdf