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.
- 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
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
int[] myArray = new int[10]; // 10 array components createdint[0]; // access an array component
4. Method parameters
void repeat(String message) { // method parameterSystem.out.println(message);}
5. Constructor parameters
new MyClass();
) or explicit constructor invocation (this();
or super();
in a constructor body).class Person {public Person(String name) { // constructor parameter} }
6. Lambda parameters
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
void myMethod() { int i = 1; // local variable }
if (myObj instanceof String myObjAsString) { //myObjAsString is a pattern variable
What are the default values for each type of variable ?
- 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