Sitemap

Understanding Static Blocks, Static Variables, Static Methods, and Static Nested Classes in Java

4 min readOct 11, 2024

--

Java provides the static keyword to allow certain members of a class to belong to the class itself rather than to instances of the class. Here's a breakdown of what that means, using the following code example:

class OuterClass {
// Static variable
static int staticVar = 10;

// Static block
static {
System.out.println("Static block is executed.");
staticVar = 20; // Modifying the static variable inside the static block
}

// Static method
static void staticMethod() {
System.out.println("Static method is called.");
System.out.println("Value of staticVar: " + staticVar);
}

// Static nested class
static class StaticNestedClass {
void display() {
System.out.println("Inside the static nested class.");
}
}

public static void main(String[] args) {
// Calling static method
OuterClass.staticMethod();

// Creating an object of the static nested class
OuterClass.StaticNestedClass nestedObj = new OuterClass.StaticNestedClass();
nestedObj.display();
}
}

1. Static Variable

A static variable is shared across all instances of a class. It is associated with the class itself, not with any object of the class.

static int staticVar = 10;
  • In the above example, staticVar is initialized with the value 10.
  • Since it’s static, the same staticVar will be shared among all instances of OuterClass.
  • This variable is modified in the static block, setting its value to 20.

Key points:

  • Memory is allocated for static variables when the class is loaded.
  • The value of staticVar can be changed, and the new value will be reflected across all uses of the class.

2. Static Block

A static block is a block of code that gets executed once when the class is loaded into memory, before the main method or any object creation.

class OuterClass {
// Static variable
static int staticVar = 10;

// Static block
static {
System.out.println("Static block is executed.");
staticVar = 20; // Modifying the static variable inside the static block
}

// Static method
static void staticMethod() {
System.out.println("Static method is called.");
System.out.println("Value of staticVar: " + staticVar);
}

// Static nested class
static class StaticNestedClass {
void display() {
System.out.println("Inside the static nested class.");
}
}

public static void main(String[] args) {
// Calling static method
// OuterClass.staticMethod();

// Creating an object of the static nested class
// OuterClass.StaticNestedClass nestedObj = new OuterClass.StaticNestedClass();
// nestedObj.display();
}
}
  • This block runs as soon as the class is loaded, modifying the value of staticVar to 20 before any methods are called.
  • It’s useful for initializing static variables or performing setup tasks that are required once for the class.

Key points:

  • Static blocks are executed in the order they appear in the class.
  • They are called only once, even if multiple instances of the class are created.

3. Static Method

A static method belongs to the class rather than any object, which means it can be called without creating an instance of the class.

static void staticMethod() {
System.out.println("Static method is called.");
System.out.println("Value of staticVar: " + staticVar);
}

    Get Debadatta’s stories in your inbox

    Join Medium for free to get updates from this writer.

  • In the example, staticMethod() prints the current value of staticVar.
  • This method can access static variables and call other static methods directly, but it cannot access instance variables or methods without creating an instance of the class.

Key points:

  • Static methods can be called without an object using the class name: OuterClass.staticMethod().
  • They are often used for utility functions like Math.max(), which do not require object state.

4. Static Nested Class

A static nested class is a static class defined inside another class. Since it's static, it can be instantiated without an instance of the outer class.

  • Here, StaticNestedClass can be instantiated using OuterClass.StaticNestedClass nestedObj = new OuterClass.StaticNestedClass();.
  • It behaves like a top-level class but is logically grouped within OuterClass.
  • The static nested class cannot access non-static members of the outer class, but it can access static ones.

Key points:

  • Static nested classes are often used to logically group classes that are only used by their outer class.
  • They do not have a reference to an instance of the outer class.

Summary of Execution Flow

When the above code is run:

  1. The class is loaded into memory.
  2. The static block is executed first, setting staticVar to 20.
  3. The staticMethod() is called, printing the updated value of staticVar (which is now 20).
  4. Finally, an instance of the StaticNestedClass is created, and its display() method is called.

Use Cases

  • Static Variables: Useful for constants or values that should be shared among all instances, like a counter tracking the number of objects created.
  • Static Blocks: Ideal for initializing static variables or running setup code that needs to be executed once when the class is loaded.
  • Static Methods: Suitable for utility or helper methods that don’t depend on instance data.
  • Static Nested Classes: Handy for encapsulating classes that don’t need access to the outer class’s instance members but still belong to its scope logically.

With this understanding, you can leverage the static keyword effectively in your Java applications for memory management, utility methods, and better code organization!

--

--

Debadatta
Debadatta

Written by Debadatta

Cloud Computing, Java SpringBoot Developor