Static keyword in Java

Static Keyword :

Introduction:

In Java, the static keyword is employed to declare class-level members (fields and methods) that are associated with the class itself rather than with individual objects. By using static, you can access these members directly using the class name without needing to create an instance of the class. As a result, static members are shared across all instances of the class, making them accessible and modifiable universally within the class context.

The static keyword is utilized in Java for the following:

1)variables

2)methods 

3)block 

4)nested class

static variable: static variable is a variable which is declared as static within a class, it becomes a class variable or a static variable.There will be only one copy of this variable that is shared by all instances of the class.static variables are initialized only once, at the start of the program execution, and they retain their values until the program terminates. Syntax: static dataType variableName;

static variable example:

class MyClass {
    static int staticVar = 42;
}
static methods: static methods are methods which are declared as static it becomes a class method or a static method.static methods can be invoked using the class name without creating an instance of the class.They cannot access instance-level members (non-static variables or methods) directly since they don't have access to any specific instance.
Syntax: static returnType methodName(parameters) { ... }.

static method example:
class MathUtils {
    static int add(int a, int b) {
        return a + b;
    }
}
static block : A static block is a special block that is executed only once when the class is loaded into memory.It is used to perform any static initialization tasks or actions that need to be executed before the class is used.

static block syntax:
static {
    // Static initialization code
}
static block example:
class MyClass {
    static int staticVar;

    static {
        // This block will be executed only once during class loading
        staticVar = 10;
    }
}
static nested class: In Java, you can define a class inside another class, and if that inner class is declared as static, it is known as a static nested class.A static nested class can be accessed using the outer class name without instantiating the outer class.Static nested classes are typically used when a class is only relevant to its outer class and does not require an instance of the outer class to exist. Syntax: static class nestedclassname { ... }

static nested class example:
class OuterClass {
    // Static nested class
    static class InnerClass {
        // Class members and methods
    }
}
Benifits of static keyword:

1.Shared Data: Static variables are shared among all instances of a class.

2.Memory Efficiency: Static variables are allocated memory only once for the entire class.

3.Utility Methods: Static methods can be called without creating an instance of the class.

4.Constants: It is commonly used to define constants with fixed values.

5.Static Initialization: Allows one-time setup tasks during class loading.

Disadvantages of static keyword :

1.Global State: Static variables can introduce global state, making code harder to maintain.

2.Testing: Static methods can be difficult to mock during unit testing.

3.Thread Safety: Shared static data can lead to thread-safety issues.

4.Code Reusability: Static methods cannot be overridden in subclasses, limiting code extensibility.

5.Coupling and Dependency: Using static members can create tight coupling between classes.
Next Post Previous Post