Wednesday, January 22, 2025
HomeProgrammingInstance Variable in Java

Instance Variable in Java

An instance variable is a type of variable in Java that is declared inside a class but outside any method, constructor, or block. These variables are associated with objects of the class and are stored in the heap memory when the object is created. Each instance of a class (object) has its own copy of the instance variables.

Characteristics of Instance Variables

  1. Declared Inside a Class: Outside of any method, constructor, or block.
  2. Belongs to an Object: Each object of the class has its own separate copy of the instance variable.
  3. Default Values: If not explicitly initialized, they are assigned default values:
    • Numeric types (int, float, etc.): 0
    • Boolean: false
    • Reference types: null
  4. Access Modifiers: Can be declared with private, protected, public, or package-private (default) access modifiers.
  5. Lifecycle: They exist as long as the object exists and are destroyed when the object is garbage collected.
  6. Access: Accessed through an object reference (e.g., objectName.variableName).
See also  Android: Where are Downloaded Files Saved? [closed]

Syntax

class ClassName {
    // Instance variable
    DataType variableName;

    // Example with initialization
    DataType variableName = value;
}

Example

class Car {
    // Instance variables
    String brand;
    String color;
    int speed;

    // Constructor to initialize instance variables
    public Car(String brand, String color, int speed) {
        this.brand = brand;  // `this` refers to the current object's instance variable
        this.color = color;
        this.speed = speed;
    }

    // Method to display car details
    public void displayDetails() {
        System.out.println("Brand: " + brand);
        System.out.println("Color: " + color);
        System.out.println("Speed: " + speed + " km/h");
    }
}

public class Main {
    public static void main(String[] args) {
        // Create objects (instances) of the Car class
        Car car1 = new Car("Toyota", "Red", 180);
        Car car2 = new Car("Honda", "Blue", 200);

        // Access and modify instance variables
        car1.displayDetails();
        car2.displayDetails();

        // Modify an instance variable for car1
        car1.speed = 190;
        System.out.println("Updated Speed for car1: " + car1.speed);
    }
}

Output

Brand: Toyota
Color: Red
Speed: 180 km/h
Brand: Honda
Color: Blue
Speed: 200 km/h
Updated Speed for car1: 190

Key Points

  1. Separate Memory: Each object has its own copy of the instance variables, so changing one object’s variable doesn’t affect another object’s variable.
  2. Access with this Keyword: The this keyword is used to refer to instance variables when there’s ambiguity with local variables or method parameters.
  3. Scope: Instance variables can be accessed by all methods of the class unless restricted by access modifiers.
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x