One of the most common confusions for Java beginners is understanding the difference between static and non-static variables.
In this blog, we’ll cover:
- Types of variables in Java
- What is a static variable?
- What is a non-static variable?
- A real-time example
- Memory behavior
- Interview-ready explanation
Let’s get started
Types of Variables in Java
- Java mainly has three types of variables:
Local Variable
- Declared inside a method, constructor, or block
- Accessible only within that method
- Destroyed once the method execution ends
Example:
public void display() {
int x = 10; // Local variable
}
Static Variable (Class Variable)
- Declared using the static keyword
- Belongs to the class, not the object
- Only one copy is shared among all objects
Non-Static Variable (Instance Variable or Object Variable)
- Declared inside a class but outside any method
- Belongs to an object
- A separate copy is created for each object
Example Program
public class Institute {
static String name = "Payilagam"; // static variables
static String address = "Velachery";
String course_name; // Non-static variables
int course_fees;
String class_duration;
public static void main(String args[]) {
System.out.println(Institute.name);
System.out.println(Institute.address);
System.out.println();
Institute java = new Institute();
java.course_name = "Java";
java.course_fees = 23000;
java.class_duration = "3 Months";
Institute python = new Institute();
python.course_name = "Python";
python.course_fees = 22000;
python.class_duration = "2 Months";
System.out.println("Course Name: " + java.course_name);
System.out.println("Course Fees: " + java.course_fees);
System.out.println("Course Duration: " + java.class_duration);
System.out.println();
System.out.println("Course Name: " + python.course_name);
System.out.println("Course Fees: " + python.course_fees);
System.out.println("Course Duration: " + python.class_duration);
}
}
Static Variable Explained
static String name = "Payilagam";
static String address = "Velachery";
Key Points:
- Belongs to the class
- Only one copy exists in memory
- Shared by all objects
- No need to create an object to access it
Accessing Static Variables:
Inside the same class, a static variable can be accessed in two ways:
System.out.println(name); // Works
System.out.println(Institute.name); // Also works
Both are valid. However, using the class name is considered a good practice.
Why?
- It clearly shows that the variable belongs to the class, not to an object.
- It improves code readability.
- It avoids confusion with local or instance variables.
- It follows Java coding standards.
Best Practice:
Always access static variables using:
ClassName.variableName
Even though both ways work, using the class name makes your code cleaner and more professional.
Non-Static Variable Explained
String course_name;
int course_fees;
String class_duration;
Key Points:
- Belongs to the object
- Each object has its own copy
- Requires object creation to access
Example:
Institute java = new Institute();
java.course_name = "Java";
Memory Concept (Important for Interviews)
Static Variables:
- Loaded into memory when the class is loaded
- Only one copy exists
Non-Static Variables:
- Loaded into memory when an object is created
- Each object has its own copy
Real-Life Analogy
Institute Name → Common for all courses → Static
Course Details → Different for each course → Non-Static
- If data is common → Use static
- If data is unique per object → Use non-static
Interview One-Line Answer
A static variable belongs to the class and is shared by all objects, while a non-static variable belongs to an object and each object has its own separate copy.
Conclusion
Use static variables when the data is common across all objects.
Use non-static variables when the data should be unique for each object.
Understanding this concept clearly will strengthen your foundation in Java and OOPS.
Top comments (0)