life time of instance variables
The scope and lifetime of instance variables in Java are tied to the lifetime of the objects they belong to. Instance variables are declared within a class but outside of any method, constructor, or block. Each instance of the class (i.e., each object) has its own copy of the instance variables. Here are the key points regarding the scope and lifetime of instance variables:
Table of Contents
Scope of Instance Variables
- 1. Instance Level:
- Instance variables are associated with instances (objects) of the class.
- They are accessible from anywhere within the class in which they are declared, as well as from methods, constructors, and blocks within the class.
- 2. Access from Instance Methods:
- Instance variables can be accessed directly from instance methods (non-static methods) without using the `this` keyword.
- 3. Access from Constructors:
- Instance variables can be accessed and initialized within constructors to set their initial values when objects are instantiated.
- 4. Access from Other Objects:
- Instance variables can be accessed from other objects of the same class, provided they have access (e.g., through inheritance or access modifiers).
Lifetime of Instance Variables
- 1. Lifetime tied to Object:
- The lifetime of instance variables is tied to the lifetime of the objects they belong to.
- Instance variables are created when an object is instantiated and cease to exist when the object is garbage-collected.
- 2. Created on Object Creation:
- Instance variables are initialized when an object is created (i.e., when a constructor is invoked).
- They maintain their values as long as the object is alive.
- 3. Memory Management:
- Instance variables consume memory for each object instance, and the memory is released when the object is no longer referenced and becomes eligible for garbage collection.
Example:
Consider a simple class `Person` with an instance variable `name`:
Syntax
```java
public class Person {
String name; // Instance variable
// Constructor
public Person(String name) {
this.name = name; // Initialize instance variable
}
// Instance method accessing instance variable
public void displayInfo() {
System.out.println("Name: " + name);
}
public static void main(String[] args) {
// Create objects of Person class
Person person1 = new Person("Alice");
Person person2 = new Person("Bob");
// Access instance variables through objects
person1.displayInfo();
person2.displayInfo();
}
}
```
In this example:
- The `name` instance variable is accessible within the `Person` class, including its constructor and instance method `displayInfo()`.
- Each object of the `Person` class (`person1` and `person2`) has its own copy of the `name` instance variable.
- The instance variables `name` of `person1` and `person2` have distinct values (“Alice” and “Bob”, respectively).
- The lifetime of the `name` instance variables is tied to the lifetime of the `Person` objects (`person1` and `person2`). They exist as long as the objects exist and are garbage-collected when the objects are no longer referenced.