Static Members and their Execution Control Flow

Java 10 min min read Updated: Mar 31, 2026 Intermediate
Static Members and their Execution Control Flow
Intermediate Topic 16 of 25

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.

Key Concept: Static members belong to the class, not to objects. They are loaded when the class is loaded into memory, and their execution flow begins before object creation.

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

java class Student { static String college = "ABC College"; String name; Student(String name) { this.name = name; } void display() { System.out.println(name + " - " + college); } }

In this example, college is common for all student objects.

Using Static Variable

java public class Main { public static void main(String[] args) { Student s1 = new Student("Amit"); Student s2 = new Student("Riya"); s1.display(); s2.display(); } }

Output

text Amit - ABC College Riya - ABC College

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

java class Demo { static void show() { System.out.println("Static method called"); } }

Calling Static Method

java public class Main { public static void main(String[] args) { Demo.show(); } }

Output

text Static method called

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

java class Demo { static int x = 10; static void show() { System.out.println(x); } }

This works because both x and show() are static.

Example: Invalid Access

java class Demo { int x = 10; static void show() { // System.out.println(x); // Error } }

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

java class Demo { static { System.out.println("Static block executed"); } }

Example

java class Demo { static { System.out.println("Static block executed"); } static void show() { System.out.println("Static method executed"); } } public class Main { public static void main(String[] args) { Demo.show(); } }

Output

text Static block executed Static method executed

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

java class Company { static String companyName; static { companyName = "Edugators Pvt Ltd"; System.out.println("Company initialized"); } }

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

java class Outer { static class Inner { void show() { System.out.println("Inside static nested class"); } } } public class Main { public static void main(String[] args) { Outer.Inner obj = new Outer.Inner(); obj.show(); } }

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:

  1. Static variables are identified and memory is allocated
  2. Static variables are assigned default values
  3. Static variable initializers and static blocks execute in source code order
  4. Then the main() method starts if it is the entry point
Remember: Static members execute during class loading, before object creation. Their execution follows the order in which they appear in the source file.

Example of Static Execution Flow

java class Demo { static int a = 10; static { System.out.println("Static block 1"); System.out.println("a = " + a); } static int b = 20; static { System.out.println("Static block 2"); System.out.println("b = " + b); } public static void main(String[] args) { System.out.println("Main method"); } }

Expected Output

text Static block 1 a = 10 Static block 2 b = 20 Main method

Explanation

  • a is initialized first
  • Then first static block executes
  • b is 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.

java class Demo { static int x; static { System.out.println("Before explicit assignment: " + x); x = 50; System.out.println("After explicit assignment: " + x); } public static void main(String[] args) { } }

Output

text Before explicit assignment: 0 After explicit assignment: 50

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.

java class Demo { static { System.out.println("Static block 1"); } static { System.out.println("Static block 2"); } public static void main(String[] args) { System.out.println("Main method"); } }

Output

text Static block 1 Static block 2 Main method

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.

java class Demo { static { System.out.println("Class loaded"); } static void show() { System.out.println("Static method"); } } public class Main { public static void main(String[] args) { Demo.show(); } }

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.

java class Demo { int x = 10; static void show() { Demo d = new Demo(); System.out.println(d.x); } }

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.

java class Demo { static int x = 100; void display() { System.out.println(x); } }

Static Final Variables

Static variables are often combined with final to create constants.

java class Constants { static final double PI = 3.14159; }

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.

java class Employee { static String companyName = "Edugators Solutions"; String employeeName; Employee(String employeeName) { this.employeeName = employeeName; } static void showCompanyPolicy() { System.out.println("All employees must follow company policy"); } void display() { System.out.println(employeeName + " works at " + companyName); } } public class Main { public static void main(String[] args) { Employee.showCompanyPolicy(); Employee e1 = new Employee("Amit"); Employee e2 = new Employee("Riya"); e1.display(); e2.display(); } }

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.

Quick Summary: Static members are class-level members that load during class loading, are shared by all objects, and follow a specific execution control flow based on source code order.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators