Java Static Keyword - Variables, Methods, Blocks and Nested Classes

⏱️ 8 min read • Beginner Level • Lesson 47

Lesson 47 of 124 of Java Tutorial

The static keyword in Java is used to create class-level members. A static member belongs to the class, not to a specific object.

Before learning static, you should understand Java class and objects, Java class members, and Java constructors.


What is static in Java?

In Java, static is a non-access modifier. It is used with variables, methods, blocks, and nested classes.

A static member belongs to the class. That means it can be accessed using the class name without creating an object.

Simple Meaning: Static means class-level. It is shared by all objects of the class.
  • Static members are not specific to individual objects.
  • Static variables are shared by all objects.
  • Static methods can be called using the class name.
  • Static blocks execute once when the class is loaded.
  • Static nested classes are nested classes declared with static.

Types of Static Members in Java

A Java class can have the following static members:

Static Member Meaning
Static Variable A class-level variable shared by all objects.
Static Method A class-level method that can be called without creating an object.
Static Block A block that runs once when the class is loaded.
Static Nested Class A nested class declared using the static keyword.

Static Variable in Java

A variable declared with the static keyword is called a static variable. It is also called a class variable.

  • Static variable belongs to the class.
  • Only one copy of a static variable is created per class.
  • All objects share the same static variable.
  • It is useful for common properties shared by all objects.
Real-Life Example: In a student class, schoolName can be static because all students may belong to the same school.

Static Variable Example

In this example, x is an instance variable, so every object gets its own copy. But y is static, so it is shared by all objects.

StaticVariableExample.java
Copy Try Download
class StaticVariableExample {
    int x;
    static int y;

    void counter() {
        x++;
        y++;

        System.out.println("x: " + x);
        System.out.println("y: " + y);
    }

    public static void main(String[] args) {
        StaticVariableExample obj1 = new StaticVariableExample();
        obj1.counter();

        StaticVariableExample obj2 = new StaticVariableExample();
        obj2.counter();
    }
}

Output:

x: 1
y: 1
x: 1
y: 2

Explanation

  1. x is an instance variable, so each object has its own copy.
  2. y is static, so it is shared by all objects.
  3. When obj1.counter() runs, x becomes 1 and y becomes 1.
  4. When obj2.counter() runs, its own x becomes 1, but shared y becomes 2.
Static variable memory representation in Java

Static Method in Java

A method declared with the static keyword is called a static method. It belongs to the class and can be called using the class name.

  • A static method can directly access static variables and static methods.
  • A static method cannot directly access non-static variables or non-static methods.
  • An object is required to access non-static members inside a static method.
  • this and super cannot be used inside a static method.
  • A static method cannot be declared as abstract.
Real-Life Example: A calculator's addition method does not depend on any particular calculator object. Such utility operations are often implemented as static methods.

Static Method Example

StaticMethodExample.java
Copy Try Download
class StaticMethodExample {
    int x = 10;
    static int y = 20;

    static void show() {
        System.out.println("Static method called");
        System.out.println("y: " + y);

        StaticMethodExample obj = new StaticMethodExample();
        System.out.println("x: " + obj.x);
    }

    public static void main(String[] args) {
        StaticMethodExample.show();

        // Static method can also be called directly inside same class
        show();
    }
}

Output:

Static method called
y: 20
x: 10
Static method called
y: 20
x: 10
Execution Order:
  1. Static variables are initialized.
  2. Static blocks execute.
  3. Main method executes.
Best Practice: Call static methods using class name, like StaticMethodExample.show(). Calling static methods using objects is allowed but not recommended.

Static Block in Java

A static block, also called a static initializer block, is used to initialize static data. It executes automatically when the class is loaded.

  • Static block executes before the main() method.
  • Static block executes only once.
  • It is commonly used for static initialization.
StaticBlockExample.java
Copy Try Download
class StaticBlockExample {
    static {
        System.out.println("Static block executed");
    }

    public static void main(String[] args) {
        System.out.println("Main method executed");
    }
}

Output:

Static block executed
Main method executed

Static Nested Class in Java

A class declared inside another class with the static keyword is called a static nested class.

A static nested class can access static members of the outer class directly. To access non-static members, it needs an object of the outer class.

StaticNestedClassExample.java
Copy Try Download
class Outer {
    static String message = "Hello from static member";

    static class Nested {
        void show() {
            System.out.println(message);
        }
    }

    public static void main(String[] args) {
        Outer.Nested obj = new Outer.Nested();
        obj.show();
    }
}

Output:

Hello from static member

static final Variable in Java

A variable declared with static final is usually used to create constants. It belongs to the class and its value cannot be changed after initialization.

StaticFinalExample.java
Copy Try Download
class Circle {
    static final double PI = 3.14159;

    public static void main(String[] args) {
        System.out.println("PI: " + Circle.PI);
    }
}

Output:

PI: 3.14159

Static Members vs Instance Members

Static Members Instance Members
Belong to the class. Belong to objects.
Shared by all objects. Each object has its own copy.
Can be accessed using class name. Accessed using object reference.
Loaded when class is loaded. Created when object is created.

When Should We Use static?

  • When data should be shared by all objects.
  • For utility methods that do not depend on object state.
  • For constants using static final.
  • For common counters, configurations, and application-wide settings.
Interview Question:

Can we override a static method in Java?

Answer: No. Static methods belong to the class, not to objects. A static method in a child class hides the parent method instead of overriding it.

Common Mistakes with static

  • Trying to access non-static members directly from a static method.
  • Using this or super inside a static method.
  • Calling static methods using object reference instead of class name.
  • Thinking each object has a separate copy of static variables.
  • Overusing static members and making code less object-oriented.
Summary:
  • The static keyword creates class-level members.
  • Static variables are shared by all objects of the class.
  • Static methods can be called using the class name.
  • Static methods can directly access only static members.
  • Static blocks execute once when the class is loaded.
  • Static nested classes can access static members of the outer class directly.
  • static final is commonly used for constants.

Frequently Asked Questions

The static keyword in Java is used to create class-level members. Static members belong to the class, not to individual objects.

A static variable is a class-level variable shared by all objects of the class.

A static method belongs to the class and can be called using the class name without creating an object.

No, a static method cannot access non-static variables directly. It needs an object reference.

No, this and super cannot be used inside a static method because static methods do not belong to an object.

A static block is used to initialize static data and executes only once when the class is loaded.

A static final variable is a class-level constant whose value cannot be changed after initialization.

Next step: Learn Java Instance Members

🚀 Continue to Java Instance Members →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Static-keyword | Language: Java

⚠️ No quiz found for static-keyword

🎉 Great job! Continue learning Java OOP step by step.