Variables in Java: Instance, Static, and Local Variables
Introduction
Variables are one of the most important building blocks of any Java program.
Whenever we write a program, we need to store information such as:
- Student name
- Student age
- Student ID
Java uses variables to store these values.
For example:
int age = 20;
Here:
int → data type
age → variable name
20 → value stored in the variable
But all variables in Java are not the same.
Variables can be classified in different ways depending on their data type, scope, purpose, lifetime, and how they are stored in memory.
How Can We Classify Variables?#
Java variables can be classified in two common ways.
Variables
│
├── Based on Data Type
│ ├── Primitive Variables
│ └── Reference Variables
│
└── Based on Scope and Purpose
├── Instance Variables
├── Static Variables
└── Local Variables
Let's understand these classifications one by one.
Variables Based on Data Type#
1. Primitive Variables#
A primitive variable stores a single primitive value.
For example:
int x = 10;
Here, x directly stores the value 10.
Some examples are:
int age = 20;
boolean passed = true;
These variables store simple values directly.
2. Reference Variables#
A reference variable refers to an object.
For example:
Student s = new Student();
Here:
Student → class/data type
s → reference variable
new Student() → creates a Student object
The variable s refers to the created object.
So, we can remember:
Primitive variables store primitive values, while reference variables refer to objects.
Variables Based on Scope and Purpose#
Now let's focus on the three important types of variables:
Variables
│
├── Instance Variables
├── Static Variables
└── Local Variables
These variables differ mainly in:
- Where they are declared
- Their scope
- Their lifetime
- Their memory behavior
Let's understand each one in detail.
Instance Variables#
What is an Instance Variable?#
An instance variable is a variable that:
- Is declared inside a class
- Is declared outside methods, constructors, and blocks
- Belongs to an object
- Gets a separate copy for every object
For example:
class Student {
int id;
String name;
}
Here:int id;String name;
are instance variables.
They are declared inside the class but outside any method.
Instance Variables Belong to Objects#
Suppose we create two Student objects:
Student s1 = new Student();
Student s2 = new Student();
Each object gets its own copy of the instance variables.
For example:
s1.id = 101;
s1.name = "Ravi";
s2.id = 102;
s2.name = "Priya";
Now we have:
s1 → id = 101, name = Ravi
s2 → id = 102, name = Priya
The values are different because each object has its own copy.
If we change s1.id:
s1.id = 999;
Then:
s1.id → 999
s2.id → 102
Changing the instance variable of one object does not change the instance variable of another object.
Where Are Instance Variables Stored?#
For beginner-level understanding, instance variables are considered part of the object stored in Heap Memory.
When we create an object using new:
Student s1 = new Student();
an object is created in the heap.
The instance variables are part of that object.
When Are Instance Variables Created?#
Instance variables are created when their object is created.
For example:
Student s1 = new Student();
When the Student object is created, its instance variables are also created.
So, the lifetime of an instance variable is associated with the lifetime of its object.
Accessing Instance Variables#
An instance variable belongs to an object.
Therefore, when we are inside a static method such as main(), we cannot directly access an instance variable without an object reference.
Consider:
class Test {
int x = 10;
public static void main(String[] args) {
// System.out.println(x);
}
}
The above statement gives a compile-time error because x is an instance variable and main() is a static method.
We need to create an object first.
class Test {
int x = 10;
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
Output:
10
Here:
t.x means: Access the x variable belonging to object t.
Accessing Instance Variables Inside Instance Methods#
Instance variables can be accessed directly inside instance methods.
For example:
class Test {
int x = 10;
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
t.m1();
}
public void m1() {
System.out.println(x);
}
}
Output:
10
10
Inside the instance method m1(), we can directly write:
System.out.println(x);
because m1() is an instance method.
Default Values of Instance Variables#
One important feature of Java is that instance variables automatically receive default values.
If we do not explicitly initialize an instance variable, Java provides a default value when the object is created.
Some common default values are:
| Data Type | Default Value |
|---|---|
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0 |
| float | 0.0 |
| double | 0.0 |
| char | '\u0000' |
| boolean | false |
| Reference types | null |
Static Variables#
What is a Static Variable?#
A static variable is a variable declared using the static keyword at the class level.
For example:
class Student {
static String collegeName = "PVP";
}
Here:
static String collegeName = "PVP";
is a static variable.
Unlike an instance variable, a static variable belongs to the class and is shared by all objects of that class.
Why Do We Need Static Variables?#
Suppose a college has 100 students.
All students belong to the same college:
Student 1 → PVP
Student 2 → PVP
Student 3 → PVP
...
Student 100 → PVP
If we store the college name separately inside every student object, the same value is repeated.
Instead, we can declare it as static:
class Student {
int id;
String name;
static String collegeName = "PVP";
}
Now collegeName is shared by all Student objects.
Therefore:
A static variable is useful when one value needs to be shared across all instances of a class.
Static Variables Are Shared#
Consider the following example:
class Test {
int x = 10;
static int y = 20;
public static void main(String[] args) {
Test t1 = new Test();
t1.x = 777;
t1.y = 888;
Test t2 = new Test();
System.out.println(t2.x + "---" + t2.y);
}
}
Output:
10---888
Why did this happen?
x is an instance variable.
Each object gets its own copy.
So:
t1.x = 777
t2.x = 10
But y is a static variable.
There is only one shared value:
y = 888
Therefore, both t1 and t2 see the same value of y.
Conceptually:
Test Class
│
static y = 888
┌─────────┴─────────┐
↓ ↓
t1 object t2 object
x = 777 x = 10
This is one of the most important differences between instance and static variables.
Where Are Static Variables Stored?#
For beginner-level understanding, static variables are associated with the class-level memory area, commonly explained using the Method Area.
When Is a Static Variable Created?#
A static variable is associated with the class when the class is loaded by the JVM. This is different from an instance variable, whose lifecycle is associated with an object.
How Can We Access Static Variables?#
A static variable can be accessed in multiple ways.
Consider:
class Test {
static int x = 10;
public static void main(String[] args) {
System.out.println(Test.x);
System.out.println(x);
Test t = new Test();
System.out.println(t.x);
}
}
Output:
10
10
10
All three statements work in this example.
Using the Class Name - Test.x
This is the recommended and clearest way.
It makes it clear that x belongs to the class.
Directly Using the Variable Name - x
This can be used when accessing the static variable from the same class.
Using an Object Reference - t.x
This can also work, but it is not the preferred way.
Default Values of Static Variables
Static variables also receive default values if they are not explicitly initialized.
For example:
class Test {
static int count;
static double price;
static boolean active;
static String name;
}
Conceptually, their default values are:
count → 0
price → 0.0
active → false
name → null
Static variables receive their default values as part of their class-level initialization.
Local Variables#
What is a Local Variable?#
A local variable is a variable declared inside:
- A method
- A constructor
- A loop
- A block
Local variables are generally used for temporary purposes.
For example:
class Test {
public static void main(String[] args) {
int x = 10;
System.out.println(x);
}
}
Here:
int x = 10;
is a local variable because it is declared inside the main() method.
Scope of a Local Variable#
A local variable has a limited scope.
For example:
public static void main(String[] args) {
int x = 10;
System.out.println(x);
}
The variable x can be accessed within the scope where it is declared.
Consider another example:
public static void main(String[] args) {
if (true) {
int x = 10;
System.out.println(x);
}
// System.out.println(x); // Compile-time error
}
Here, x is declared inside the if block.
Therefore, it cannot be accessed outside that block.
So we can remember:
The scope of a local variable is limited to the method, constructor, loop, or block in which it is declared.
When Are Local Variables Created and Destroyed?#
Local variables are temporary.
For example:
if (true) {
int x = 10;
System.out.println(x);
}
The variable x is required only inside that block.
Once execution leaves the block, x is no longer accessible.
Where Are Local Variables Stored?#
For beginner-level understanding, local variables are commonly associated with Stack Memory.
Local Variables Do Not Get Default Values#
This is a very important difference.
The JVM does not automatically assign default values to local variables.
For example:
class Test {
public static void main(String[] args) {
int x;
System.out.println(x);
}
}
This produces a compile-time error because x has not been initialized.
We must initialize it before using it:
class Test {
public static void main(String[] args) {
int x;
x = 10;
System.out.println(x);
}
}
Output:
10
A local variable must be initialized before its first use.
Instance vs Static vs Local Variables#
Now let's compare all three types.
| Feature | Instance Variable | Static Variable | Local Variable |
|---|---|---|---|
| Declared | Inside class, outside methods | Inside class with static | Inside method/block/constructor/loop |
| Belongs to | Object | Class | Method/block |
| Copy | One copy per object | One shared copy per class | Temporary/local copy |
| Scope | Object-level | Class-level | Within declared scope |
| Lifetime | Associated with object | Associated with class | During its execution scope |
| Default value | Yes | Yes | No |
| Typical memory concept | Heap, as part of object | Class/Method Area | Stack |
| Access | Through object when required | Usually through class name | Directly within scope |
| Example | int age; | static String college; | int total = 100; |
Think about a college.
Student Class
Instance Static
Variables Variable
│ │
↓ ↓
Different for Same/shared
every student for all students
│ │
↓ ↓
id, name, age collegeName
A local variable is different.
It is something temporarily needed while performing a particular task:
Student Object
↓
Instance Data
id, name, age
Class
↓
Static Data
collegeName
Method
↓
Local Data
total, count, result
So the easiest way to remember is:
Instance = belongs to an object
Static = belongs to the class and is shared
Local = belongs to a method or block temporarily
Check Your Understanding#
1. Predict the Output#
What will be the output of the following program?
class Test {
int x = 10;
static int y = 20;
public static void main(String[] args) {
Test t1 = new Test();
t1.x = 100;
t1.y = 200;
Test t2 = new Test();
t2.x = 300;
System.out.println(t1.x);
System.out.println(t2.x);
System.out.println(t1.y);
System.out.println(t2.y);
}
}
Explain why each value is printed.
2. Find the Error#
What is wrong with this program?
class Student {
int age = 20;
public static void main(String[] args) {
System.out.println(age);
}
}
How can you correct it without making age static?
3. Identify the Variable Types#
Identify whether each variable is an instance, static, or local variable:
class Employee {
int id;
String name;
static String company = "ABC";
public void display() {
double salary = 50000;
}
}
Also explain:
- Which variables have separate copies for different objects?
- Which variable is shared?
- Which variable exists only inside the method?
4. Think About the Design#
Suppose you are creating a Student class with these values:
- Student ID
- Student name
- College name
- A temporary value used only while calculating marks
Which should be:
- Instance variable?
- Static variable?
- Local variable?
Explain why you would choose each type.