🔥 What is Variable in Java? Static Variable & Types of Variables Explained
Introduction
If you are starting your journey with Java programming, one of the first concepts you’ll come across is
variables. Think of a variable like a small container inside your computer’s memory that stores data. Without variables, writing any meaningful Java program would be impossible.
In this blog, we’ll break down:
What is a variable in Java?
What is a static variable?
The different types of variables in Java with examples.
Let’s dive in! 🚀
What is a variable in Java?
What is a static variable?
The different types of variables in Java with examples.
💡 What is a Variable in Java?
A variable in Java is simply a name that points to a memory location where data is stored.
👉 In simple words:
Variable = Name of a container.
Value = Content stored inside the container.
Variable = Name of a container.
Value = Content stored inside the container.
Example:
int age = 25;
String name = "John";
Here:
age is a variable that stores an integer value (25).
name is a variable that stores a string value (“John”).
age is a variable that stores an integer value (25).
name is a variable that stores a string value (“John”).
⚡ Types of Variables in Java
In Java, variables are mainly classified into three categories:
1. Local Variables
Declared inside a method, constructor, or block.
Accessible only within that block.
Automatically destroyed once the method is finished.
✅ Example:
public class Demo {
public void showMessage() {
int number = 10; // local variable
System.out.println("Number: " + number);
}
}
Declared inside a method, constructor, or block.
Accessible only within that block.
Automatically destroyed once the method is finished.
2. Instance Variables
Declared inside the class but outside any method.
Each object gets its own copy of the instance variable.
Often used to store object-specific properties.
✅ Example:
public class Student {
String name; // instance variable
int age; // instance variable
}
Declared inside the class but outside any method.
Each object gets its own copy of the instance variable.
Often used to store object-specific properties.
3. Static Variables (Class Variables)
Declared using the keyword static.
Shared among all objects of the class (only one copy exists).
Useful for values that should be common for all objects.
✅ Example:
public class Student {
String name;
int age;
static String schoolName = "ABC School"; // static variable
}
👉 Here, schoolName will be the same for all students.
Declared using the keyword static.
Shared among all objects of the class (only one copy exists).
Useful for values that should be common for all objects.
🔑 Why Use Static Variables?
Static variables are powerful when you want to save memory and keep values consistent across all objects.
✔️ Advantages:
Saves memory (one copy for all objects).
Easy to access without creating an object.
👉 Example:
public class Counter {
static int count = 0;
Counter() {
count++;
System.out.println("Object number: " + count);
}
public static void main(String[] args) {
new Counter();
new Counter();
new Counter();
}
}
Output:
Object number: 1
Object number: 2
Object number: 3
Saves memory (one copy for all objects).
Easy to access without creating an object.