STATIC AND NON-STATIC MEMBERS | BCA SEM 04 JAVA BKNMU JUNAGADH

Static and Non-Static Members in Java

Static and Non-Static Members in Java

In Java, static and non-static members are key concepts that differ in their behavior and usage. Here’s a breakdown of both:

1. Static Members:

Static members belong to the class rather than to any specific instance of the class. This means they are shared among all instances (objects) of the class. Static members can be accessed without creating an instance of the class.

Characteristics:

  • Declaration: A static member is declared using the static keyword.
  • Access: Static members can be accessed directly using the class name, without creating an object.
  • Lifetime: Static members exist as long as the class is loaded into memory (i.e., the entire program’s runtime).
  • Memory Allocation: Only one copy of a static member is shared across all instances of the class.
  • Usage: Typically used for constants, utility methods, or fields that are common to all instances.

Example:

        
class MyClass {
    static int counter = 0; // static variable

    static void increment() { // static method
        counter++;
    }
}

public class Test {
    public static void main(String[] args) {
        MyClass.increment(); // Accessing static method without creating an object
        System.out.println(MyClass.counter); // Output will be 1
    }
}
        
    

2. Non-Static Members:

Non-static members belong to an instance of the class. Each object (instance) of the class has its own copy of these members.

Characteristics:

  • Declaration: A non-static member is declared without the static keyword.
  • Access: Non-static members must be accessed through an instance of the class.
  • Lifetime: Non-static members exist as long as the object is alive (i.e., until the object is destroyed).
  • Memory Allocation: Each object of the class gets its own copy of non-static members.
  • Usage: Typically used when the value of the member is specific to each instance of the class.

Example:

        
class MyClass {
    int counter = 0; // non-static variable

    void increment() { // non-static method
        counter++;
    }
}

public class Test {
    public static void main(String[] args) {
        MyClass obj1 = new MyClass();
        MyClass obj2 = new MyClass();

        obj1.increment();
        obj2.increment();
        
        System.out.println(obj1.counter); // Output will be 1
        System.out.println(obj2.counter); // Output will be 1
    }
}
        
    

Key Differences:

Aspect Static Members Non-Static Members
Association Belong to the class. Belong to instances of the class.
Access Can be accessed using the class name. Must be accessed through an object.
Memory Only one copy for the entire class. Each object has its own copy.
Lifetime Exists as long as the class is loaded. Exists as long as the object exists.
Usage Used for shared or common behavior. Used for object-specific behavior.

Conclusion:

Static members are useful for behavior that should be shared across all instances, such as counters or constants.
Non-static members are useful for data that is unique to each object, like instance variables that hold different values for different objects.

Post a Comment

Thanks for comment.

Previous Post Next Post