Slide 1
Slide 2
Slide 3

Instance variables in java programming (V7)

No Comments

 

Instance Variables

  • Instance variables are declared in a class but outside any method, constructor, or block.
  • When a space is allocated for an object in the heap, a slot for each instance variable value is created.
  • Instance variables are created when an object is created using the keyword 'new' and are destroyed when the object is destroyed or closed.
  • Instance variables hold values that must be referenced by more than one method, constructor, or block, or are essential parts of the object’s state that must be present throughout the class.
  • Instance variables can be declared at the class level, either before or after use.
  • Access and modification of instance variables can be controlled.
  • Instance variables can be used by all methods, constructors, and blocks within the class. Generally, it is recommended to declare variables as private (access level). However, they can be used in subclasses that may modify the values of these variables.
  • Instance variables have default values. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned either during declaration or within a constructor.
  • Instance variables can be accessed directly by calling the variable name within the class. However, within static methods (when instance variables are made accessible), they should be called using the fully qualified name, for example: ObjectReference.VariableName.

        In Java, instance variables are variables declared inside a class but outside any method, constructor, or block. They are associated with an instance (object) of the class, meaning each object has its own copy of the instance variables.

Source Code:

Copied!
public class InstanceVariable {
	// Instance variables
    String name;  // Default value: null
    int age;      // Default value: 0

    // Constructor to initialize instance variables
    public InstanceVariable(String name, int age) {
        this.name = name; // 'this' refers to the instance variable
        this.age = age;
        
    }

public static void main(String[] args) {
        // Create an object of the InstanceVariable class
        InstanceVariable person1 = new InstanceVariable("Alice", 30);
        InstanceVariable person2 = new InstanceVariable("Bob", 25);

        // Access instance variables using ObjectReference.VariableName
        System.out.println("Your Name : "+person1.name);
        System.out.println("Your Age : "+person1.age);
        System.out.println("Your Name : "+person2.name);
        System.out.println("Your Age : "+person2.age);
        
        // Modify instance variables
        person1.age = 37;
        System.out.println("\nYour Name : "+person1.name);
        System.out.println("Your Age : "+person1.age);
    }	
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top