Static Keyword in OOP

Understanding static variables, methods, and blocks in Object-Oriented Programming

What is Static Keyword?

The static keyword in Java is used for memory management mainly. It can be applied to variables, methods, blocks, and nested classes. The static keyword belongs to the class rather than the instance of the class. Static members are shared among all instances of the class.

Static Keyword Kya Hai? (Hinglish)

Java mein static keyword memory management ke liye mainly use hota hai. Ye variables, methods, blocks, aur nested classes par apply kiya ja sakta hai. Static keyword class ka hota hai, class ke instance ka nahi. Static members class ke saare instances mein share hote hain.

Features of Static Keyword

  • Static Variables (Class Variables)
  • Static Methods (Class Methods)
  • Static Blocks (Initialization Blocks)
  • Static Nested Classes
  • Static Import

Example Program

// Class demonstrating static keyword
class Employee {
    // Instance variables
    private String name;
    private int id;
    
    // Static variable (shared among all instances)
    private static int totalEmployees = 0;
    
    // Static block (executed when class is loaded)
    static {
        System.out.println("Employee class is loaded");
    }
    
    // Constructor
    public Employee(String name, int id) {
        this.name = name;
        this.id = id;
        totalEmployees++; // Incrementing static variable
    }
    
    // Instance methods
    public String getName() {
        return name;
    }
    
    public int getId() {
        return id;
    }
    
    // Static method
    public static int getTotalEmployees() {
        return totalEmployees;
    }
    
    // Static nested class
    public static class Department {
        private String name;
        private static int totalDepartments = 0;
        
        public Department(String name) {
            this.name = name;
            totalDepartments++;
        }
        
        public String getName() {
            return name;
        }
        
        public static int getTotalDepartments() {
            return totalDepartments;
        }
    }
}

// Another class with static members
class MathOperations {
    // Static method
    public static int add(int a, int b) {
        return a + b;
    }
    
    // Static method
    public static int multiply(int a, int b) {
        return a * b;
    }
    
    // Static constant
    public static final double PI = 3.14159;
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Creating Employee objects
        Employee emp1 = new Employee("John", 101);
        Employee emp2 = new Employee("Alice", 102);
        Employee emp3 = new Employee("Bob", 103);
        
        // Accessing static variable through class
        System.out.println("Total Employees: " + Employee.getTotalEmployees());
        
        // Creating Department objects
        Employee.Department dept1 = new Employee.Department("HR");
        Employee.Department dept2 = new Employee.Department("IT");
        
        // Accessing static method of nested class
        System.out.println("Total Departments: " + Employee.Department.getTotalDepartments());
        
        // Using static methods from MathOperations
        System.out.println("\nMath Operations:");
        System.out.println("Addition: " + MathOperations.add(5, 3));
        System.out.println("Multiplication: " + MathOperations.multiply(5, 3));
        System.out.println("PI value: " + MathOperations.PI);
        
        // Static import example (commented as it's not allowed in this context)
        // import static java.lang.Math.*;
        // System.out.println("Square root: " + sqrt(16));
        
        // Demonstrating static variable sharing
        System.out.println("\nEmployee Information:");
        System.out.println("Employee 1: " + emp1.getName() + " (ID: " + emp1.getId() + ")");
        System.out.println("Employee 2: " + emp2.getName() + " (ID: " + emp2.getId() + ")");
        System.out.println("Employee 3: " + emp3.getName() + " (ID: " + emp3.getId() + ")");
        System.out.println("Total Employees (accessed through instance): " + Employee.getTotalEmployees());
    }
}
                    

Program Explanation (Hinglish)

Is program mein humne static keyword ke different features ka use kiya hai:

  • Static Variables: totalEmployees variable jo saare instances mein share hota hai
  • Static Methods: getTotalEmployees() method jo class level par call hota hai
  • Static Blocks: Class load hone par execute hone wala block
  • Static Nested Classes: Department class jo Employee class ke andar static hai
  • Static Constants: MathOperations class mein PI constant

Important Points

  • Static members belong to the class, not instances
  • Static variables are shared among all instances
  • Static methods can only access static members
  • Static blocks execute when class is loaded
  • Static nested classes can be instantiated without outer class