Java variables can be classified based on where they are declared and how they belong to a class or object.
The three main types of variables are:
- Local variables
- Instance variables
- Static variables
Understanding these types is important because each has different behavior, especially regarding memory, object ownership, and accessibility.
Variable scope is another important concept. Scope determines where a variable can be accessed within a Java program.
Local Variables
A local variable is a variable declared inside a method, constructor, or block.
For example:
class Student {
void display() {
int age = 20;
System.out.println(age);
}
}
Here, age is a local variable because it is declared inside the display() method.
The variable belongs to that particular method and can only be accessed within its applicable scope.
Example of a Local Variable
public class Main {
public static void main(String[] args) {
int age = 25;
System.out.println(age);
}
}
Here:
int age = 25;
is a local variable because it is declared inside the main() method.
Local Variables Exist Within Their Scope
Consider:
public class Main {
public static void main(String[] args) {
int age = 25;
System.out.println(age);
}
}
The variable age can be used inside main() after its declaration.
However, it cannot be accessed outside that method:
public class Main {
public static void main(String[] args) {
int age = 25;
}
static void display() {
System.out.println(age); // Error
}
}
The display() method cannot directly access age because age is a local variable belonging to main().
Local Variables Must Be Initialized Before Use
A local variable does not automatically receive a default value.
For example:
public class Main {
public static void main(String[] args) {
int age;
System.out.println(age);
}
}
This produces a compilation error because age has not been definitely assigned a value before it is used.
Correct:
public class Main {
public static void main(String[] args) {
int age;
age = 25;
System.out.println(age);
}
}
Or:
int age = 25;
Local Variables in Blocks
Local variables can also be declared inside blocks such as if, for, and while.
For example:
if (true) {
int number = 10;
System.out.println(number);
}
Here, number is available inside that block.
It cannot be accessed after the block ends:
if (true) {
int number = 10;
}
System.out.println(number); // Error
The variable’s scope ended when the block ended.
Instance Variables
An instance variable is a variable declared inside a class but outside methods, constructors, and blocks, and without the static modifier.
For example:
class Student {
String name;
int age;
}
Here:
String name;
int age;
are instance variables.
Each object created from the Student class has its own instance variables.
Example of Instance Variables
class Student {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student();
student1.name = "Karim";
student1.age = 22;
student2.name = "Rahul";
student2.age = 24;
System.out.println(student1.name);
System.out.println(student2.name);
}
}
Output:
Karim
Rahul
Both objects have their own name and age variables.
Conceptually:
student1
name → Karim
age → 22
student2
name → Rahul
age → 24
Changing the instance variable of one object does not normally change the corresponding instance variable of another object.
Instance Variables Belong to Objects
Instance variables are associated with individual objects.
For example:
class Car {
String color;
}
If we create three objects:
Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();
each object has its own color variable.
car1.color = "Red";
car2.color = "Blue";
car3.color = "Black";
The objects can therefore hold different values.
Instance Variables Get Default Values
Unlike local variables, instance variables receive default values when an object is created if no explicit initializer is provided.
For example:
class Student {
int age;
double marks;
boolean passed;
String name;
}
The default values are approximately:
| Data Type | Default Value |
|---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | '\u0000' |
boolean | false |
| Reference types | null |
For example:
class Student {
int age;
String name;
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
System.out.println(student.age);
System.out.println(student.name);
}
}
Output:
0
null
The int instance variable receives 0, while the String reference receives null.
Static Variables
A static variable is a variable declared inside a class using the static keyword.
Static variables belong to the class, rather than to individual objects.
For example:
class Student {
static String college = "ABC College";
}
Here, college is a static variable.
There is one class-level variable associated with the class rather than one separate variable for every object.
Example of a Static Variable
class Student {
String name;
static String college = "ABC College";
}
public class Main {
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student();
student1.name = "Karim";
student2.name = "Rahul";
System.out.println(student1.name);
System.out.println(student2.name);
System.out.println(Student.college);
}
}
Output:
Karim
Rahul
ABC College
The name variable belongs to each object, while college is shared at the class level.
Accessing a Static Variable
A static variable is normally accessed using the class name:
Student.college
For example:
class Student {
static int count = 0;
}
public class Main {
public static void main(String[] args) {
System.out.println(Student.count);
}
}
Using the class name makes it clear that the variable belongs to the class.
Static Variables Are Shared
Consider:
class Student {
static String college = "ABC College";
}
Suppose we create two objects:
Student student1 = new Student();
Student student2 = new Student();
Both objects refer to the same class-level college variable.
If the value is changed:
Student.college = "XYZ College";
the class-level value becomes:
XYZ College
for accesses through the class.
This is different from instance variables, where every object has its own value.
Static Variables for Shared Data
Static variables are useful when a value should be associated with the class rather than with each individual object.
For example:
class Employee {
static String companyName = "ABC Technologies";
String employeeName;
}
Every employee can have a different employeeName, while all employees can share the same companyName.
Local vs Instance vs Static Variables
The three variable types can be compared as follows:
| Feature | Local Variable | Instance Variable | Static Variable |
|---|---|---|---|
| Declared | Inside method, constructor, or block | Inside class, outside methods/constructors/blocks | Inside class with static |
| Belongs to | Method/block execution | Individual object | Class |
static required | No | No | Yes |
| Separate copy per object | No | Yes | No, generally one class-level variable |
| Default value | No for local variables | Yes | Yes |
| Must be initialized before use | Yes | Not necessarily | Not necessarily |
| Access | Within its scope | Through object/context | Usually through class name |
| Lifetime | During applicable execution | Associated with object lifetime | Associated with class initialization/class lifetime |
Variable Scope
Variable scope refers to the part of a Java program where a variable can be accessed directly by its name.
In simple terms:
Scope determines where a variable is visible and usable.
For example:
public class Main {
public static void main(String[] args) {
int age = 25;
System.out.println(age);
}
}
The variable age is accessible within its applicable scope inside main().
Scope of a Local Variable
A local variable’s scope begins after its declaration and extends to the end of the block in which it is declared.
For example:
public static void main(String[] args) {
int age = 25;
System.out.println(age);
}
The scope of age is the remainder of the main() block after its declaration.
Scope Inside an if Block
Consider:
if (true) {
int number = 10;
System.out.println(number);
}
number is accessible inside the if block.
It is not accessible here:
if (true) {
int number = 10;
}
System.out.println(number); // Error
The variable’s scope ended with the block.
Scope Inside a Loop
A variable declared inside a for loop is generally limited to that loop’s scope.
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Here, i can be used inside the loop.
It cannot normally be used after the loop:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
System.out.println(i); // Error
The variable i is local to the for statement’s scope.
Method Parameters Are Local Variables
Parameters declared in a method are also local to that method.
For example:
static void display(int age) {
System.out.println(age);
}
Here, age is a method parameter.
It can be accessed inside the method but not directly outside it.
static void display(int age) {
System.out.println(age);
}
public static void main(String[] args) {
display(25);
// System.out.println(age); // Error
}
Instance Variable Scope
Instance variables are declared at class level and can generally be accessed by instance methods of that class, subject to Java’s access-control rules.
For example:
class Student {
String name;
void display() {
System.out.println(name);
}
}
The method display() can directly access the instance variable name.
An object can also access it when the field is accessible:
Student student = new Student();
student.name = "Karim";
If the field is private, it cannot be accessed directly from unrelated classes:
class Student {
private String name;
}
Instead, access is typically provided through methods such as getters and setters.
Static Variable Scope
Static variables are also declared at class level.
For example:
class Student {
static String college = "ABC College";
}
The variable can be accessed using the class name:
System.out.println(Student.college);
Its accessibility still depends on the access modifier.
For example:
class Student {
private static int count;
}
The count variable cannot be directly accessed from unrelated classes because it is private.
Scope and Lifetime Are Different
Scope and lifetime are related concepts, but they are not the same.
Scope answers:
Where can I access this variable by name?
Lifetime answers:
How long does this variable exist during program execution?
For example, a local variable has a limited scope within its method or block and exists as part of that method’s execution.
An instance variable is associated with an object and exists as part of that object’s state.
A static variable is associated with the class rather than a particular object and has class-level lifetime characteristics.
Understanding this difference becomes especially useful when learning memory management and object lifecycle.
Example Combining All Three Variable Types
Consider this program:
class Student {
static String college = "ABC College";
String name;
void display(int marks) {
int result = marks + 5;
System.out.println("Name: " + name);
System.out.println("College: " + college);
System.out.println("Result: " + result);
}
}
Here:
static String college = "ABC College";
is a static variable.
String name;
is an instance variable.
int marks
is a method parameter, which is local to the method invocation.
int result = marks + 5;
is a local variable.
The program therefore contains all three major variable categories.
Common Beginner Mistakes
Thinking Every Variable Belongs to an Object
Not every variable is an instance variable.
For example:
int age = 25;
inside a method is a local variable.
A variable declared at class level without static is generally an instance variable:
class Student {
int age;
}
A class-level variable declared with static is a static variable:
class Student {
static int count;
}
Trying to Access a Local Variable Outside Its Scope
Incorrect:
void display() {
int age = 25;
}
void show() {
System.out.println(age);
}
age belongs to display() and is not directly available in show().
Assuming Local Variables Get Default Values
This is incorrect:
int age;
System.out.println(age);
A local variable must be definitely assigned before it is read.
Creating a Static Variable for Every Object
A static variable is not normally created separately for every object.
For example:
class Student {
static int count;
}
count is class-level shared state.
An instance variable is different:
class Student {
int age;
}
Each Student object has its own age state.
Frequently Asked Questions
What are the main types of variables in Java?
The main categories are local variables, instance variables, and static variables.
What is a local variable?
A local variable is declared inside a method, constructor, or block and is accessible within its applicable scope.
What is an instance variable?
An instance variable is declared inside a class but outside methods, constructors, and blocks, without static. Each object generally has its own copy.
What is a static variable?
A static variable is a class-level variable declared using static. It is associated with the class rather than with an individual object.
What is the difference between an instance variable and a static variable?
An instance variable belongs to an individual object, while a static variable belongs to the class and is shared at the class level.
Do local variables have default values?
No. Local variables must be definitely assigned before they are read.
Do instance variables have default values?
Yes. If no explicit initializer is provided, instance variables receive Java’s default values for their types.
Do static variables have default values?
Yes. Static variables also receive default values when no explicit initializer is provided.
Can a local variable be static?
A local variable cannot be declared with the static modifier.
For example, this is invalid:
void display() {
static int count = 10;
}
static variables are declared at class level.
Can an instance variable be static?
A variable declared static is a static variable, not an instance variable. The two categories are distinguished by the presence or absence of static.
What is variable scope?
Variable scope is the part of the program where a variable can be accessed directly by its name.
What is the scope of a local variable?
Its scope is generally limited to the method, constructor, or block in which it is declared, beginning after its declaration.
Can one method access another method’s local variable?
No. A local variable belongs to the scope in which it is declared.
Why are static variables useful?
Static variables are useful when a piece of state should belong to the class rather than to each individual object.
For example:
class Employee {
static String company = "ABC Technologies";
}
How do I access a static variable?
The usual form is:
ClassName.variableName
For example:
Student.college
How do I access an instance variable?
An instance variable can generally be accessed through an object reference:
Student student = new Student();
student.name = "Karim";
The exact accessibility depends on the field’s access modifier.
Are scope and lifetime the same?
No.
Scope describes where a variable can be accessed, while lifetime describes how long the variable exists.