100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

static Keyword in Java

Learn how the static keyword creates class-level fields, methods, and blocks shared across all objects in Java.

OOP BasicsBeginner9 min readJul 7, 2026
Analogies

1. Introduction

The static keyword in Java marks a member (field, method, block, or nested class) as belonging to the class itself rather than to any individual object. Static members are created once when the class is loaded into memory and are shared by all instances of that class.

🏏

Cricket analogy: The static keyword is like a franchise's team logo — it belongs to the team itself, not to any individual player, and is created once when the team is formed, shared identically by every player wearing that jersey.

Because static members exist independently of any object, they can be accessed using the class name directly, without needing to create an object first. This makes static ideal for constants, counters, and utility methods.

🏏

Cricket analogy: Because a league's official rulebook belongs to the league, you reference it directly as 'ICC Rules' without needing a specific match — just as static members are accessed via the class name directly, ideal for constants like MAX_OVERS, counters like totalMatchesPlayed, and utility methods like calculateNRR().

2. Syntax

java
class ClassName {
    static dataType staticField;

    static returnType staticMethod(parameters) {
        // static method body
    }

    static {
        // static initializer block, runs once at class loading
    }
}

// Access without creating an object
ClassName.staticField = value;
ClassName.staticMethod();

3. Explanation

A static field is shared across all objects of a class — if one object changes its value, every other object (and the class itself) sees the updated value, since there is only one copy in memory. Static methods can be called using the class name (e.g., Math.sqrt(4)), and they cannot directly access non-static (instance) fields or methods, because a static method may be called without any object existing at all.

🏏

Cricket analogy: A static field like totalTeamWins is shared by every player object — if one player's stats update it, the whole team sees the new value, since there's only one scoreboard; but a static method like Team.announceSchedule() can't reach a specific player's individual battingAverage, because the schedule can be announced before any player is even signed.

A static block is executed exactly once, when the class is first loaded by the JVM, and is typically used to initialize static fields that require more complex setup logic than a simple assignment.

🏏

Cricket analogy: A static block is like a stadium's one-time pitch preparation ritual done only when the venue is first certified for international matches — used to set up complex ground conditions before the first ball is ever bowled there.

Exam trap: A static method cannot directly reference this or call instance (non-static) methods/fields, because static methods can execute without any object instance existing. Doing so causes a compile-time error.

Static initializer blocks run once, in the order they appear, when the class is loaded — before any object of the class is created or any static method is called.

4. Example

java
public class Counter {
    static int count = 0;   // shared across all objects
    int id;

    static {
        System.out.println("Counter class loaded");
    }

    Counter() {
        count++;
        id = count;
    }

    static void showCount() {
        System.out.println("Total objects created: " + count);
        // count is static, so it's accessible directly here
    }

    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        Counter c3 = new Counter();

        System.out.println("c1 id: " + c1.id);
        System.out.println("c2 id: " + c2.id);
        System.out.println("c3 id: " + c3.id);
        Counter.showCount();
    }
}

5. Output

text
Counter class loaded
c1 id: 1
c2 id: 2
c3 id: 3
Total objects created: 3

6. Key Takeaways

  • Static members belong to the class, not to any individual object, and are shared by all instances.
  • Static members are accessed via ClassName.member, though ObjectRef.member also works (not recommended).
  • Static methods cannot directly access non-static (instance) fields or methods.
  • Static blocks execute once, when the class is loaded by the JVM.
  • Static fields are a single shared copy in memory across all objects of the class.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#StaticKeywordInJava#Static#Keyword#Syntax#Explanation#StudyNotes#SkillVeris