Local vs Instance vs Static Variables in Java

Mohammed Al-Qudah
5 min readFeb 27, 2024

--

In Java, variables serve as containers for storing data values during program execution. Each variable in Java is associated with a data type, determining the kind of value it can store. These data types can be categorized into two main groups.

1- Primitive variables: Primitive variables hold simple values directly, such as integers, floating-point numbers, booleans, and characters.

2- Reference variables: Also known as non-primitive variables, store memory addresses that point to objects in the heap memory. This includes instances of classes, arrays, interfaces, and Strings. Strings, as non-primitive data types, are particularly important for storing sequences of characters, providing extensive functionality for string manipulation and processing as I have discussed in my previous article here.

The Three Types of Variables in Java

There are three main types of variables in Java which are:

1- Local Variables: Local variables are variables declared within a specific scope. These are typically defined within methods, constructors, code blocks, loops, etc. Local variables are only accessible within the scope in which they are declared and cease to exist once the scope is exited.

2- Instance Variables: These variables are declared within a class and represent the state of individual objects. They can be accessed from any part of the code using object references. Within a method’s scope, instance variables can be accessed using an object of the class in which the variable is declared. Additionally, within other methods of the same class, instance variables can sometimes be accessed directly without the need for an object reference, as they belong to the instance of the class itself. Instance variables are unique to each instance of the class and are initialized with default values when the object is created.

3- Static Variables: Static variables are declared within a class and are characterized by the use of the static keyword. When a variable is declared as static, a single instance of that variable is created and shared among all objects of the class at the class level. Static variables are initialized only once, either at the start of the execution of the program or at the first access of the class, and they retain their values throughout the program’s execution. They are not tied to any specific instance of the class but are accessible by all instances, allowing them to store data that is common across all objects of the class. Static variables can be accessed using either the class name or an instance of the class. Additionally, static variables can also be accessed directly within the same class without the need for an object reference, as they belong to the class itself.

  • Let's consider the following example to distinguish between the different types:
public class VariableTypesExample {
int instanceVar; // Instance variable
public void myMethod() {
int localVar = 10; // Local variable
instanceVar = 0; // Accessing and modifying instance variable

System.out.println("Local variable: " + localVar); // printing localVar inside method scope
System.out.println("Instance variable: " + instanceVar);
}
static int staticVar; // Static variable

public static void main(String[] args) {
VariableTypesExample obj1 = new VariableTypesExample(); // Creating object 1
VariableTypesExample obj2 = new VariableTypesExample(); // Creating object 2

localVar = 30; // compile-time error : cannot find symbol
obj1.localVar = 30; // compile-time errors : cannot find symbol

obj1.myMethod(); // Calling the method

obj1.instanceVar = 10;
obj2.instanceVar = 20;

System.out.println("obj1.instanceVar value >>> " + obj1.instanceVar); // 10
System.out.println("obj2.instanceVar value >>> " + obj2.instanceVar); // 20

staticVar = 30; // Accessing and modifying static variable directly
obj1.staticVar = 60; // Accessing and modifying static using an object of the class
obj2.staticVar = 100; // Accessing and modifying static variable using an object of the class

System.out.println("staticVar value accessing it directly>>> " + staticVar); // 100
System.out.println("staticVar value accessing it using obj1 >>> " + obj1.staticVar); // 100
System.out.println("staticVar value accessing it using obj2 >>> " + obj2.staticVar); // 100

VariableTypesExample.staticVar = 200; // Accessing and modifying static using class name

System.out.println("staticVar value accessing it using class name: " + VariableTypesExample.staticVar); // 200
}
}
  • Beginning with the local variable inside the myMethod() method, which is defined within the class scope, it’s crucial to understand that this variable is only accessible within the method’s scope. Any attempt to access it directly or via an object of the class would result in a compile-time error in the code, as demonstrated within the main method, which fails to compile.
  • Regarding the instance variable instanceVar, it's crucial to understand that it is directly accessible within the class scope, particularly within any non-static method or constructor. However, attempting to access it directly from a static context, like the main method, would lead to a compilation error. This indicates that instanceVar is only accessible within instances (objects) of the class, not at the class level itself. Moreover, each object instantiated from the class possesses its own distinct copy of instanceVar, which is stored in separate memory locations. Consequently, modifications made to instanceVar of one object do not affect those of another object; each object preserves its unique values for instanceVar.
  • The static variable defined in the class can be accessed anywhere in the class and can be accessed directly, through an instance of the class, or using the class name. The most important thing to note is that changing the value of a staticVar affects all instances of the class since only one memory location is defined for that static variable. Therefore, modifying the value of staticVar through any object or directly using the class name will result in the same value being modified across all instances of the class.

Conclusion

In conclusion, Java programming language encompasses three primary types of variables, each serving distinct purposes within the structure of a program. Understanding the distinctions and characteristics of these variable types is essential for effective Java programming, as they facilitate the organization, access, and management of data within classes and methods. By leveraging each variable type appropriately, developers can design robust and efficient Java applications.

--

--