Static Members and Their Execution Control Flow
In Java, the keyword static is used for members that belong to the class itself rather than to any specific object. These members are called static members. Understanding static members is extremely important because they behave differently from instance members in terms of memory allocation, access, and execution flow.
Static members are widely used for shared data, utility methods, constants, and class-level initialization. In addition, interview questions often focus on the execution order of static variables, static blocks, and static methods.
What are Static Members?
Static members are class-level members declared using the static keyword. They are shared among all objects of the class and can be accessed without creating an object.
Static members include:
- static variables
- static methods
- static blocks
- static nested classes
Why Static Members are Needed
Static members are useful when some data or behavior should be common to all objects of a class.
- to store shared values
- to create utility/helper methods
- to define constants
- to perform one-time initialization
Example:
- College name for all students
- Math utility functions like
Math.sqrt() - Configuration values shared by all objects
Static Variable
A static variable is also called a class variable. It is created only once when the class is loaded, and all objects of the class share the same copy.
Example
In this example, college is common for all student objects.
Using Static Variable
Output
Since college is static, both objects use the same shared value.
How Static Variable Differs from Instance Variable
| Point | Static Variable | Instance Variable |
|---|---|---|
| Belongs to | Class | Object |
| Copies created | Only one | One per object |
| Memory allocation | At class loading time | At object creation time |
| Shared | Yes | No |
Static Method
A static method belongs to the class and can be called without creating an object. It is generally used for common utility operations.
Example
Calling Static Method
Output
Rules of Static Methods
- Static methods can directly access only static members
- They cannot directly access instance variables or instance methods
- They can be called using the class name
main()itself is a static method
Example: Static Method Accessing Static Variable
This works because both x and show() are static.
Example: Invalid Access
This causes an error because an instance variable cannot be directly accessed from a static method.
Why Static Methods Cannot Directly Access Instance Members
Static members belong to the class and exist even before any object is created. Instance members belong to objects, so they require an object reference.
Since static methods can run without any object, they cannot directly use object-specific data.
Static Block
A static block is a block declared with the static keyword inside a class. It is executed only once when the class is loaded into memory.
Syntax
Example
Output
The static block runs first because it executes when the class is loaded.
Use of Static Block
Static blocks are generally used for one-time initialization such as:
- setting static variables
- loading configuration
- loading drivers
- performing class-level setup
Example: Static Initialization
Static Nested Class
A static nested class is a class declared inside another class using the static keyword. It behaves like a static member of the outer class.
Example
Static nested classes can be created without creating an object of the outer class.
Execution Control Flow of Static Members
One of the most important interview topics in Java is the execution control flow of static members.
Static execution begins when the JVM loads the class into memory. The order is generally:
- Static variables are identified and memory is allocated
- Static variables are assigned default values
- Static variable initializers and static blocks execute in source code order
- Then the
main()method starts if it is the entry point
Example of Static Execution Flow
Expected Output
Explanation
ais initialized first- Then first static block executes
bis initialized next- Then second static block executes
- Finally,
main()runs
Static Variable with Default Value Before Initialization
Static variables first get default values before explicit initialization happens.
Output
Here, x first gets default value 0, then is explicitly assigned 50.
Multiple Static Blocks
A class can have more than one static block. They execute in the same order in which they appear.
Output
Static Members and Object Creation
Static members do not depend on object creation. Even if no object is created, static blocks and static methods can still execute when the class is referenced.
Here, the class is loaded and the static block runs even without object creation.
Can Static Members Access Non-Static Members?
Not directly. But they can access non-static members through an object reference.
This works because the instance variable is accessed through an object.
Can Non-Static Members Access Static Members?
Yes. Non-static methods and constructors can directly access static members because static members belong to the class and are already available.
Static Final Variables
Static variables are often combined with final to create constants.
These are commonly used for fixed values shared across the program.
Real-World Example of Static Members
Suppose every employee in a company belongs to the same organization name, and you want a utility method to display the company policy.
Common Mistakes
- Trying to access instance variables directly inside static methods
- Thinking static variables are copied for every object
- Assuming static blocks execute every time an object is created
- Using objects to access static members unnecessarily
- Confusing class loading time with object creation time
Best Practices
- Use static variables only for truly shared data
- Use static methods for utility logic not dependent on object state
- Use static final for constants
- Keep static blocks simple and limited to one-time initialization
- Access static members using class name for better readability
Interview-Oriented Points
- Static members belong to the class, not to objects
- Static variables are created only once
- Static methods can directly access only static members
- Static block executes once when the class is loaded
- Execution flow of static members follows source code order
main()is a static method- Static members can be accessed without object creation
Conclusion
Static members are a core part of Java class design. They are useful for shared data, utility methods, constants, and one-time initialization. Since they belong to the class, their behavior is very different from instance members.
Understanding static execution control flow is especially important because it explains how Java loads classes, initializes static data, and begins program execution before any object is created.

