Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Static Variables in Classes



In 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.

Static vs Instance Variables

Before diving deep, let's distinguish:

Syntax of Static Variables

class MyClass {
  static int count = 0; // Static variable
}

Example: Sharing a Static Variable Across Objects

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

Explanation

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.

Accessing Static Variables

You can access static variables using:

  1. Class name (preferred)
  2. Instance name (allowed, but not recommended)
System.out.println(Student.studentCount); // Recommended
System.out.println(s1.studentCount);      // Valid but not ideal

Why Use Static Variables?

They come in handy when you need:

Another Example: Static Variable for Configuration

class AppConfig {
    static String ENV = "PRODUCTION";
}

public class ConfigTest {
    public static void main(String[] args) {
        System.out.println("Environment: " + AppConfig.ENV);
    }
}
Environment: PRODUCTION

What Happens If You Modify Static Variable?

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

Do Static Variables Work in Inheritance?

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

Common Pitfalls

Summary

Real-Life Analogy

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.

Practice Task

Try this:

Doing is learning — go for it!

QUIZ

Question 1:What best describes a static variable in Java?

Question 2:Each object of a class has its own copy of the static variable.

Question 3:Which of the following are true about static variables in Java?

Question 4:What is the output of the following program?
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);
    }
}

Question 5:Static variables in Java can be accessed via object references, but it's not considered best practice.

Question 6:In which of the following situations would using a static variable be appropriate?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You 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.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M