static variable vs instance variable in Java

1. Introduction

In Java, variables are categorized into different types based on their scope and lifetime. Two such categories are static variables and instance variables. A static variable is associated with the class itself rather than with any specific instance of the class. In contrast, an instance variable is associated with a specific instance of a class, and each instance has its own copy of that variable.

2. Key Points

1. static variables are also known as class variables and are shared across all instances of the class.

2. instance variables are specific to an instance and each object has its own copy.

3. static variables are initialized when the class is loaded into memory.

4. instance variables are initialized each time a new object is created.

3. Differences

Static Variable Instance Variable
Shared by all instances of the class. Each instance has its own copy.
Stored in the class memory area. Stored in heap memory for each object.
Can be accessed directly using the class name. Can only be accessed through a class instance.
Only one copy exists regardless of the number of instances of the class. Each object will have its own copy of the instance variable.

4. Example


// Define a class with a static variable and an instance variable
class Counter {
    static int staticCount = 0; // static variable
    int instanceCount = 0; // instance variable

    // Constructor increments both static and instance variables
    public Counter() {
        staticCount++;
        instanceCount++;
    }
}

public class Main {
    public static void main(String[] args) {
        // Step 1: Create multiple instances of Counter class
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        Counter c3 = new Counter();

        // Step 2: Access and print static and instance variables
        System.out.println("Static count from c1: " + c1.staticCount);
        System.out.println("Instance count from c1: " + c1.instanceCount);

        System.out.println("Static count from c2: " + c2.staticCount);
        System.out.println("Instance count from c2: " + c2.instanceCount);

        System.out.println("Static count from c3: " + c3.staticCount);
        System.out.println("Instance count from c3: " + c3.instanceCount);
    }
}

Output:

Static count from c1: 3
Instance count from c1: 1
Static count from c2: 3
Instance count from c2: 1
Static count from c3: 3
Instance count from c3: 1

Explanation:

1. The class Counter has both a static variable (staticCount) and an instance variable (instanceCount).

2. Each time a new instance of Counter is created, staticCount is incremented once and shared among all instances, while instanceCount is incremented once per object and not shared.

3. The output shows that staticCount has a value of 3 for all instances, reflecting the three instances created, while instanceCount always has a value of 1, unique to each instance.

5. When to use?

- Use a static variable when you need a class-level variable that is shared across all instances of the class.

- Use an instance variable when each instance should have its own copy of the variable, with its own distinct value.

Comments