What is a Static Variable?
A static variable belongs to the class, not to individual objects.
It is declared using the static keyword, and only one copy of the variable exists in memory, regardless of how many objects are created.
Characteristics of Static Variables
- Declared using the
statickeyword. - Belongs to the class.
- Only one copy exists.
- Shared by all objects.
- Memory is allocated only once when the class is loaded.
- Accessed using the class name.
Syntax
class Student {
static String institute = "Payilagam";
}
Accessing the static variable:
System.out.println(Student.institute);
Real-Life Example
Imagine the employees working in a company.
Every employee has a different name and employee ID, but the company name printed on every ID card is the same.
Employee 1
Name : Mohan
Company : Payilagam
Employee 2
Name : Deva
Company : Payilagam
Employee 3
Name : Madhavan
Company : Payilagam
Since the company name is common to every employee, it is a perfect example of a static variable.
What is a Non-Static (Instance) Variable?
A non-static variable, also called an instance variable, belongs to an individual object.
Whenever a new object is created, Java allocates a separate copy of every instance variable for that object.
Characteristics of Instance Variables
- Declared without the
statickeyword. - Belongs to an object.
- Every object has its own copy.
- Memory is allocated whenever an object is created.
- Accessed through an object reference.
Syntax
class Student {
String name;
int age;
}
Accessing an instance variable:
Student s = new Student();
System.out.println(s.name);
Real-Life Example
Each employee has a unique:
- Name
- Employee ID
- Age
These values are different for every employee, so they should be stored as non-static variable(instance variables).
Example Program
public class Building {
static int age = 10;
static String name = "Payilagam";
String studName1 = "Mohan";
int studAge1 = 22;
String studName2 = "Deva";
int studAge2 = 23;
String studName3 = "Madhavan";
int studAge3 = 24;
public static void main(String[] args) {
Building student1 = new Building();
Building student2 = new Building();
Building student3 = new Building();
System.out.println(Building.name);
System.out.println(Building.age);
System.out.println(student1.studName1 + " " + student1.studAge1);
System.out.println(student1.studName2 + " " + student1.studAge2);
System.out.println(student1.studName3 + " " + student1.studAge3);
}
}
Understanding the Program
1. Why are static variables accessed using the class name?
System.out.println(Building.name);
System.out.println(Building.age);
Here, name and age are declared as static.
Since they belong to the class itself, there is no need to create an object to access them.
This is why we write:
Building.name
instead of
student1.name
2. Why can't a static method access instance variables directly?
The main() method is declared as static.
A static method belongs to the class, whereas instance variables belong to individual objects.
So if we write:
System.out.println(studName1);
Java doesn't know which object's studName1 should be accessed.
Therefore, the compiler displays the error:
non-static variable studName1 cannot be referenced from a static context
3. How do we access instance variables?
First, create an object.
Building student1 = new Building();
Now access the variable using that object.
System.out.println(student1.studName1);
Here,
-
student1is the object. -
studName1belongs to that object.
The Problem in This Program:
Although the program works, it violates one of the most important principles of Object-Oriented Programming.
We created three objects.
Building student1 = new Building();
Building student2 = new Building();
Building student3 = new Building();
But every object contains all three students.
student1
Mohan
Deva
Madhavan
student2
Mohan
Deva
Madhavan
student3
Mohan
Deva
Madhavan
Every object stores the same information.
This defeats the purpose of creating multiple objects.
One Object Should Represent One Real-World Entity
One of the core principles of Object-Oriented Programming is:
One object should represent one real-world entity.
Instead of storing multiple students inside one object, each object should represent exactly one student.
Student Object 1
Name : Mohan
Age : 22
Student Object 2
Name : Deva
Age : 23
Student Object 3
Name : Madhavan
Age : 24
This design is cleaner, easier to maintain, and follows object-oriented principles.
How can we solve this problem:
By using Constructor in java we can solve this problem let discuss this in next blog.
Summary
| Static Variable | Instance Variable |
|---|---|
| Belongs to the class | Belongs to an object |
Declared using the static keyword |
Declared without the static keyword |
| Only one copy exists | Every object has its own copy |
| Shared by all objects | Unique for every object |
| Memory allocated once when the class loads | Memory allocated whenever an object is created |
Accessed using ClassName.variable
|
Accessed using object.variable
|
| Stores common data | Stores object-specific data |

Top comments (0)