A static variable is a variable declared inside a class using the static keyword. Unlike an instance variable, which belongs to each individual object, a static variable belongs to the class itself and is shared by all objects of that class.
This makes static variables useful when one piece of data should have a common value across all objects. A simple example is a school name shared by every student in the same school.
Why Do We Need Static Variables?
Suppose a Java application creates thousands of student objects, and every student belongs to the same school. Storing the school name separately inside every object would represent the same information repeatedly.
A static variable provides a better model because there is only one shared variable for that class.
public class Student {
String name;
static String school = "ABC School";
}
Here, name is an instance variable, so every student can have a different name. The school variable is static, so all Student objects can share the same school value.
Basic Syntax
The general syntax for declaring a static variable is:
static dataType variableName;
For example:
static String companyName; static int totalStudents; static double taxRate;
You can also assign an initial value during declaration:
static String companyName = "ABC Technologies"; static double taxRate = 18.0;
Static Variable Example
public class Student {
String name;
static String school = "ABC School";
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student();
student1.name = "Rahul";
student2.name = "Anita";
System.out.println(student1.name);
System.out.println(student2.name);
System.out.println(Student.school);
}
}
The two students have different names because name is an instance variable. Both students use the same school value because school is static.
The key idea is simple: instance variables represent data that can differ between objects, while static variables represent data shared at the class level.
Accessing a Static Variable
The preferred way to access a static variable is through the class name.
public class Student {
static String school = "ABC School";
public static void main(String[] args) {
System.out.println(Student.school);
}
}
Here, Student.school clearly communicates that school belongs to the Student class rather than to a particular Student object.
Changing a Static Variable
Because a static variable is shared, changing its value affects the value seen by other objects that access the same variable.
public class Student {
String name;
static String school = "ABC School";
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student();
student1.name = "Rahul";
student2.name = "Anita";
Student.school = "XYZ School";
System.out.println(student1.name + " - " + Student.school);
System.out.println(student2.name + " - " + Student.school);
}
}
Both students now see XYZ School. There is not one separate school variable for each object. There is one static variable associated with the class.
Static Variable vs Instance Variable
| Point | Static Variable | Instance Variable |
|---|---|---|
| Keyword | Declared using static | Declared without static |
| Belongs To | Class | Object |
| Copies | One shared class-level variable | Each object normally has its own variable |
| Access | ClassName.variableName | object.variableName |
| Use | Shared data | Object-specific data |
A Practical Example: Counting Objects
Static variables are especially useful when you need to maintain a value shared across all objects. A common example is counting how many objects have been created.
public class Student {
String name;
static int count = 0;
Student(String name) {
this.name = name;
count++;
}
public static void main(String[] args) {
Student student1 = new Student("Rahul");
Student student2 = new Student("Anita");
Student student3 = new Student("Amit");
System.out.println(Student.count);
}
}
Every time the constructor creates a Student object, count increases by one. Because count is static, all Student objects update the same variable.
After creating three objects, the value of Student.count is 3.
Why Not Use an Instance Variable for the Counter?
If count were an instance variable, every Student object would receive its own counter. Each object would then track its own value instead of maintaining one total for the class.
Student student1 = new Student("Rahul");
Student student2 = new Student("Anita");
student1.count;
student2.count;
That is not what we want for a total object count. The requirement is one shared counter, which is exactly what a static variable provides.
Static Variables and the Class Name
Java allows a static variable to be accessed through an object reference in some situations, but using the class name is clearer and communicates the actual ownership.
public class Student {
static String school = "ABC School";
public static void main(String[] args) {
System.out.println(Student.school);
}
}
The expression Student.school immediately tells another developer that the value is shared at the class level.
Remember: When you see ClassName.variableName, think about data that belongs to the class rather than to one particular object.
Static Variables and Default Values
Static variables, like instance variables, receive default values when they are declared without an explicit initial value.
public class Main {
static int number;
static String name;
static boolean active;
public static void main(String[] args) {
System.out.println(number);
System.out.println(name);
System.out.println(active);
}
}
The default values are determined by the variable's data type. The integer receives 0, the reference variable receives null, and the boolean receives false.
Static Variables and Memory
At a conceptual level, a static variable represents class-level state rather than a separate piece of state for every object. When many objects use the same value, this model avoids representing that shared state as independent object data.
The exact JVM memory implementation is more nuanced than simply saying that every static variable lives in one particular memory area. For beginners, the important distinction is ownership: static means class-level state.
Common Beginner Mistakes
- Thinking a static variable belongs to every object separately.
- Using a static variable when every object actually needs a different value.
- Forgetting that changing a shared static variable changes the value seen through the class.
- Confusing static variables with local variables.
- Accessing static data through an object and then assuming the data belongs to that object.
When Should You Use a Static Variable?
Use a static variable when the information logically belongs to the class and should be shared by its objects.
| Situation | Suitable Variable |
|---|---|
| Every student has a different name | Instance variable |
| Every employee has a different salary | Instance variable |
| All students belong to the same school | Static variable |
| All products use the same tax rate | Static variable, when the rate is class-wide |
| Need to count objects created from a class | Static variable |
Interview Insight
If an interviewer asks, "What is a static variable in Java?", a strong answer is: A static variable is a class-level variable declared using the static keyword. It is associated with the class rather than an individual object, so objects of that class share the same static variable.
A common follow-up question is, "What is the difference between a static and instance variable?" The essential answer is that an instance variable represents object-specific state, while a static variable represents state shared at the class level.
Quick Revision
| Concept | Key Point |
|---|---|
| Static Variable | A class-level variable declared using static. |
| Ownership | Belongs to the class rather than an individual object. |
| Sharing | Objects of the same class can access the same static variable. |
| Access | Preferred form is ClassName.variableName. |
| Instance Difference | Instance variables hold object-specific state; static variables hold shared class-level state. |
| Common Use | Shared configuration, counters, constants, and other class-wide data. |
Static variables become powerful when you stop thinking only in terms of individual objects and start thinking about information that belongs to the class as a whole. The key question is simple: Does every object need its own value, or should all objects share one value? If the value is truly shared, a static variable may be the right choice.
