life time of class variables in java

life time of class variables in java

The scope and lifetime of class variables, also known as static variables, in Java differ from those of instance variables. Class variables are associated with the class itself rather than with individual instances of the class. Here are the key points regarding the scope and lifetime of class variables:

life time of class variables in java

Scope of Class Variables

  • 1. Class Level:
    • Class variables are declared with the `static` keyword within a class but outside of any method, constructor, or block.
    • They are associated with the class itself rather than with any particular instance of the class.
  • 2. Access from Anywhere:
    • Class variables are accessible from anywhere within the class in which they are declared, including from static and instance methods, constructors, and blocks.
  • 3. Access from Outside the Class:
    • Class variables can also be accessed directly using the class name, even without creating an instance of the class.

Lifetime of Class Variables

  • 1. Lifetime tied to Class:
    • The lifetime of class variables is tied to the lifetime of the class itself.
    • They are created when the class is loaded into memory by the JVM and exist until the class is unloaded.
  • 2. Created at Class Loading:
    • Class variables are initialized when the class is loaded into memory, before any objects of the class are created.
  • 3. Shared Among Instances:
    • Since class variables are shared among all instances of the class, changes made to a class variable by one instance are visible to all other instances.
  • 4. Memory Management:
    • Class variables consume memory once per class, regardless of the number of instances created. They are allocated memory in the method area (also known as the static area) of the JVM’s memory.

Example:

Consider a simple class `Counter` with a class variable `count`:

Syntax
```java
public class Counter {
    static int count; // Class variable

    // Constructor
    public Counter() {
        count++; // Increment class variable
    }

    // Static method to display count
    public static void displayCount() {
        System.out.println("Count: " + count);
    }

    public static void main(String[] args) {
        // Create objects of Counter class
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        Counter c3 = new Counter();

        // Access class variable through class name
        Counter.displayCount(); // Outputs: Count: 3
    }
}
```

In this example:

  • The `count` class variable is accessible within the `Counter` class, including its constructor and static method `displayCount()`.
  • Each object of the `Counter` class (`c1`, `c2`, `c3`) shares the same copy of the `count` class variable.
  • The class variable `count` is incremented each time a `Counter` object is created.
  • The lifetime of the `count` class variable is tied to the lifetime of the `Counter` class itself. It is created when the class is loaded into memory and exists until the class is unloaded.