Loading
In Java, the static keyword is used to indicate that a particular member belongs to the class itself, rather than to any specific object created from that class. Static members can be variables, methods, blocks, or even nested classes. They play a key role in memory management and allow you to share data or behavior across all instances of a class.


Static Variable

A static variable is a variable that is declared with the static keyword inside a class but outside any method, constructor, or block. It is also known as a class variable.


Key Points

  • There is only one copy of a static variable for the entire class, regardless of how many objects are created.
  • All instances of the class share the same static variable.
  • Static variables are initialized only once, at the start of program execution.
  • They help save memory and are commonly used for constants or counters.



Syntax:

static data_type variable_name = value;


Example:

package quipohouse;

public class StaticVariable {
    static int a = 10; // Static Variable

    public int getStaticVariable() {
        return StaticVariable.a; // Accessing by class name
    }

    public static void main(String[] args) {
        StaticVariable s = new StaticVariable();
        System.out.println(s.getStaticVariable());
        System.out.println(StaticVariable.a);
    }
}


Output:

10 10


Static Method

A static method is a method that is declared with the static keyword. It belongs to the class rather than to any specific object.


Key Points

  • Static methods can be called using the class name, without creating an object.
  • They can also be called directly from within other static methods of the same class.
  • Static methods can only access other static members (variables and methods) of the class.
  • They are commonly used for utility or helper methods.


Syntax:

static return_type method_name() {
    // method body
}


Example:

package quipohouse;

public class StaticMethod {

    public static void getStaticMethod() { // Static Method
        System.out.println("Inside Static Method");
    }

    public static void main(String[] args) {
        StaticMethod.getStaticMethod(); // Calling by class name
        getStaticMethod();              // Calling directly
    }
}


Output:

Inside Static Method Inside Static Method


Static Block

A static block is a block of code inside a class that is marked with the static keyword. It is used to initialize static variables or to perform operations that need to be executed only once when the class is loaded.


Key Points

  • A static block is executed automatically when the class is loaded, before the main method runs.
  • It is executed only once during the lifetime of the program.
  • It is commonly used for static initialization.

Syntax:

static {
    // block body
}


Example:

package quipohouse;

public class StaticVariable {

    public static void getStaticVariable() {
        System.out.println("Inside Static Method");
    }

    // Static Block
    static {
        System.out.println("Executed before Main Method");
    }

    public static void main(String[] args) {
        getStaticVariable(); // Static method can be called directly
    }
}


Output:

Executed before Main Method Inside Static Method


Static Variable  Vs.  Static Method  Vs.  Static Block

Static MemberPurposeCalled ByWhen Executed
Static VariableShared data for all objects of the classClass name/objectWhen class is loaded
Static MethodUtility or helper functions, operates on static data onlyClass name/directWhen called 
Static BlockStatic initialization, one-time setup before main method startsAutomaticallyWhen class is loaded


Key Points

  • Static members belong to the class, not to any object.
  • Static variables and methods can be accessed without creating an instance.
  • Static blocks are executed once, when the class is loaded into memory.


Static members give you the power to share data and functionality across your entire class. Use them wisely for things that truly belong to the class not to individual objects. They’re your best friends for utility methods, global counters, and efficient memory management.