Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 28

static Keyword in Java

In this lesson: Learn static in Java — static variables, methods, blocks and nested classes — with memory diagram and output. In Hindi & English.

What does static mean

The static keyword makes a variable or method belong to the class itself, not to individual objects. A static member is shared by all objects, and you can use it through the class name without creating any object.

💡 static = one shared copy at the class level; non-static = a separate copy in each object.

Static variables

class Counter {
    static int count = 0;   // shared by all objects
    Counter() { count++; }  // increases the shared count
}
// use:
new Counter(); new Counter(); new Counter();
System.out.println(Counter.count);
3

There is only one count, shared by every object. Each new object increases the same variable, so it correctly counts how many were created — accessed via the class name.

Static methods

class MathUtil {
    static int square(int n) {
        return n * n;
    }
}
// call without an object:
System.out.println(MathUtil.square(5));
25

A static method is called on the class, not an object — MathUtil.square(5). This is why utility methods and main are static.

Static blocks

class Config {
    static String appName;
    static {
        appName = "CodingEasily";   // runs once at class load
        System.out.println("Static block ran");
    }
}
Static block ran

A static { } block runs once when the class is first loaded, before any object exists. It is handy for setting up static variables that need more than a simple value.

static vs instance

Pointstaticinstance
Belongs toThe classEach object
CopiesOne sharedOne per object
Accessed viaClass nameObject
Needs an objectNoYes

When to use static

  • Shared data that all objects use, like a counter or a constant.
  • Utility methods that do not depend on object data, like Math.max.
  • Constants, often combined as static final.

Use static when something belongs to the class as a whole, not to one object.

Common mistakes

  • Using instance variables in a static method. A static method has no object, so it cannot.
  • Expecting per-object copies of a static variable. There is only one, shared by all.
  • Overusing static. Too much static breaks object-oriented design; use it only when shared.
  • Calling static via an object. It works but is confusing — call it via the class name.

Summary

  • static makes a member belong to the class, not each object.
  • A static variable is one shared copy across all objects.
  • A static method is called on the class, without an object.
  • A static block runs once when the class loads.
  • Use static for shared data, utility methods and constants.

Next, control visibility with access modifiers, or organise code with Java methods.

Frequently Asked Questions

What does the static keyword do in Java?
static makes a variable or method belong to the class itself, not to individual objects. A static member is shared by all objects and can be used without creating one, via the class name.
What is a static variable?
A static variable is shared across all objects of a class — there is only one copy. For example, a counter of how many objects were created. Changing it from any object affects all of them.
What is the difference between static and instance members?
An instance member belongs to each object and each object has its own copy. A static member belongs to the class, shared by all objects, and exists even before any object is created.
Can a static method use instance variables?
No, not directly. A static method has no specific object, so it cannot use non-static (instance) variables or call non-static methods without an object. It can only use static members directly.
What is a static block?
A static block runs once when the class is first loaded, before any object is created. It is used to initialise static variables that need some setup code beyond a simple value.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicthis and super Your progress: 28 / 61 (46%) · Lesson 29 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free