Static Variables, Methods, and Blocks

The keyword static is used to denote that a variable, method, or block is associated with the class rather than instances (objects) of the class.

Static Variables

Static variables are class-level variables that are shared among all instances of the class. This means that rather than each object having its own copy of the variable, all objects of the class will access the same variable. A static variable is initialized only once when the class is loaded into memory, which can improve memory efficiency.

Features of Static Variables

  • Shared by All Instances: Static variables are shared among all objects of the class.
  • Memory Efficiency: Only one copy of a static variable exists, regardless of how many objects of the class are created.
  • Accessed Using Class Name: Static variables are typically accessed using the class name, although they can also be accessed by object references.

Syntax:

In this example, the count variable is static, so even though we create two objects (c1 and c2), both share the same variable. The value of count is incremented each time an object is created.

Static Methods

A static method belongs to the class rather than any specific object. Static methods can only access other static variables or methods. They cannot directly access instance variables or instance methods since they don’t have an object to operate on.

Features of Static Methods

  • No Access to Instance Members: Static methods cannot access non-static variables or methods.
  • Accessed Using Class Name: Static methods are commonly invoked using the class name, but they can also be called from an object reference.
  • Useful for Utility Methods: Static methods are often used for operations that do not depend on the state of an object, like utility functions or helper methods.

Syntax:

In this example, the square method is static, so it’s called using the class name MathUtility without needing an object.

Static Blocks

A static block (or static initialization block) is used for static initialization of a class. It’s executed only once when the class is loaded into memory, which makes it a useful tool for initializing static variables or performing class-level operations that need to be done before any object of the class is created.

Features of Static Blocks

  • Executed Once: Static blocks are executed only once, when the class is loaded into memory.
  • Used for Initialization: Typically used to initialize static variables that require more complex logic than simple assignment.
  • Cannot Be Accessed Directly: Static blocks do not have a return type and cannot be called explicitly.

Syntax:

In this example, the static block initializes the connectionString when the Database class is loaded.

When to Use Static Members

  • Static Variables: Use when a variable should be shared across all instances of a class, such as a counter, configuration values, or constants.
  • Static Methods: Use when you need methods that don’t depend on instance variables or methods. Utility methods and factory methods are common examples.
  • Static Blocks: Use static blocks for complex initialization of static variables that cannot be done in a single line of code. They’re also useful when you need to execute code when a class is loaded, such as initializing resources or setting up static configurations.