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
The static variable belongs to the class rather than any individual instance. The instance variable belongs to an individual instance of a class.
Initialized only once at the start of the execution. Initialized each time an instance of the class is created.
Only one copy exists, shared among all instances of the class. Each instance has its own copy of the variable.
It can be accessed directly by the class name and doesn’t need an object to access it. It requires an object of the class to access it.
Declared with the static keyword. Declared without the static keyword.
It can be accessed by static methods within the class. It can only be accessed by instance methods and constructors.
If changed, the new value is available to all instances of the class. Changes to the variable affect only the instance in which the change was made.

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