четверг, 4 января 2024 г.

static keyword in Java

 The 'static' keyword is used to declare elements that belong to the class rather than instances of the class. It can be applied to variables, methods, blocks and nested classes. Here's a brief overview of how 'static' is used in different contexts:


  • Static Variable

  • When a variable is declared as static within a class, it becomes a class variable, also known as a static variable.
  • There is only one copy of a static variable that is shared among all instances of the class.
  • Static variables are typically used for constants or variables that should be common to all instances of a class.
public class Counter {
// Static variable to keep track
// of the number of instances
private static int instanceCount = 0;

// Constructor increments the instance
// count and assigns a unique object number
public Counter() {
instanceCount++;
}

// Static method to get the total number
// of instances created
public static int getInstanceCount() {
return instanceCount;
}

public static void main(String[] args) {
// Creating instances of Counter
Counter obj1 = new Counter();
Counter obj2 = new Counter();
Counter obj3 = new Counter();

// Accessing static method to get the
// total number of instances
System.out.println("Total number of instances: "
+ Counter.getInstanceCount());
}
}

  • Static Methods:

    • When a method is declared as static, it belongs to the class rather than to any particular instance of the class.
    • Static methods can be called using the class name, without creating an instance of the class.
    • Static method can access static data member and can change the value of it.
    public class StaticInitializerExample {
    // Static variable to be initialized
    private static int staticVariable;

    // Static method to initialize the static variable
    public static void initializeStaticVariable(int value) {
    staticVariable = value;
    System.out.println("Static variable initialized to: "
    + staticVariable);
    }

    // Method to get the value of the static variable
    public static int getStaticVariable() {
    return staticVariable;
    }

    public static void main(String[] args) {
    // Calling the static method to initialize
    // the static variable
    StaticInitializerExample.initializeStaticVariable(42);

    // Accessing the static variable using a static method
    int retrievedValue = StaticInitializerExample.getStaticVariable();
    System.out.println("Retrieved static variable value: "
    + retrievedValue);
    }
    }

    • Static Blocks:

    • Static blocks are used to initialize static variables.
    • They are executed only once when the class is loaded into memory.
    public class StaticBlockExample {
    // Static variable to be initialized
    private static int staticVariable;

    // Static block to initialize the static variable
    static {
    staticVariable = 42;
    System.out.println("Static variable initialized in the static block: "
    + staticVariable);
    }

    // Method to get the value of the static variable
    public static int getStaticVariable() {
    return staticVariable;
    }

    public static void main(String[] args) {
    // Accessing the static variable using a static method
    int retrievedValue = StaticBlockExample.getStaticVariable();
    System.out.println("Retrieved static variable value: " + retrievedValue);
    }
    }

    • Static Nested Classes
    • A static nested class is a nested class that is declared as static.
    • It can be accessed using the class name without creating an instance of the outer class
    public class OuterClass {
    // Instance variable of the outer class
    private int outerVar;

    // Constructor for the outer class
    public OuterClass(int outerVar) {
    this.outerVar = outerVar;
    }

    // Instance method of the outer class
    public void outerMethod() {
    System.out.println("Outer Method");
    }

    // Static nested class
    public static class StaticNestedClass {
    // Static nested class can have its own members
    private int nestedVar;

    // Constructor for the static nested class
    public StaticNestedClass(int nestedVar) {
    this.nestedVar = nestedVar;
    }

    // Method of the static nested class
    public void nestedMethod() {
    System.out.println("Nested Method");
    }
    }
    }


    public class Main {
    public static void main(String[] args) {
    // Creating an instance of the outer class
    OuterClass outerInstance = new OuterClass(10);

    // Accessing instance members of the outer class
    outerInstance.outerMethod();

    // Creating an instance of the static nested class
    // without an instance of the outer class
    OuterClass.StaticNestedClass nestedInstance =
    new OuterClass.StaticNestedClass(5);

    // Accessing members of the static nested class
    nestedInstance.nestedMethod();
    }
    }

    Комментариев нет:

    Отправить комментарий