⬅ Previous Topic
Java Access ModifiersNext Topic ⮕
Java Static Methods Explained⬅ Previous Topic
Java Access ModifiersNext Topic ⮕
Java Static Methods ExplainedIn Java, a static
variable is a class-level variable — meaning it belongs to the class, not to instances (objects) of the class. All instances of a class share the same static variable. This makes static variables useful when you want to store common data across all objects of a class.
Before diving deep, let's distinguish:
class MyClass {
static int count = 0; // Static variable
}
Let’s look at a program that demonstrates how a static variable behaves across multiple objects:
class Student {
static int studentCount = 0; // Shared across all instances
String name;
Student(String name) {
this.name = name;
studentCount++; // Increments shared counter
}
void showStudent() {
System.out.println("Name: " + name);
System.out.println("Total Students: " + studentCount);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Alice");
Student s2 = new Student("Bob");
s1.showStudent();
s2.showStudent();
}
}
Name: Alice
Total Students: 2
Name: Bob
Total Students: 2
Even though we create two students, studentCount
is not duplicated. It remains a single variable shared across all Student
objects. When both constructors increment the counter, they’re modifying the same memory location.
You can access static variables using:
System.out.println(Student.studentCount); // Recommended
System.out.println(s1.studentCount); // Valid but not ideal
They come in handy when you need:
class AppConfig {
static String ENV = "PRODUCTION";
}
public class ConfigTest {
public static void main(String[] args) {
System.out.println("Environment: " + AppConfig.ENV);
}
}
Environment: PRODUCTION
If one object or the class itself modifies a static variable, the change reflects across all references.
class Counter {
static int value = 5;
}
public class Test {
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.value++;
System.out.println(c2.value); // Outputs 6
}
}
6
Yes — static variables are inherited, but they belong to the class they are defined in. They are not polymorphic. Here's an example:
class Parent {
static int staticVar = 100;
}
class Child extends Parent {}
public class InheritTest {
public static void main(String[] args) {
System.out.println(Child.staticVar); // Accessing staticVar from Parent
}
}
100
Imagine a school: each student has a name (instance variable), but they all belong to the same school (static variable). Changing the school name affects all students, no matter how many you create.
Try this:
Car
class that tracks the total number of cars created using a static variable.Doing is learning — go for it!
class Counter {
static int value = 5;
}
public class Test {
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.value++;
System.out.println(c2.value);
}
}
⬅ Previous Topic
Java Access ModifiersNext Topic ⮕
Java Static Methods ExplainedYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.