Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Class Static Methods



In Java, a static method belongs to the class, not to instances of the class. This means you can call a static method without creating an object of the class. If you've ever used Math.sqrt() or Integer.parseInt(), you've already used static methods. Static methods are useful when the behavior doesn’t depend on instance data (i.e., member variables). They're ideal for utility or helper operations.

How to Define a Static Method

To define a static method, use the static keyword before the return type:
public class MathUtils {
    public static int square(int x) {
        return x * x;
    }
}
You can call this method directly using the class name:
int result = MathUtils.square(5);
System.out.println(result);
25

Static Method vs Instance Method

- Static methods don’t need an object to be called. - Instance methods require an object to be created.
public class Hello {
    public static void sayStaticHello() {
        System.out.println("Hello from static!");
    }

    public void sayInstanceHello() {
        System.out.println("Hello from instance!");
    }

    public static void main(String[] args) {
        // Calling static method
        Hello.sayStaticHello();

        // Calling instance method
        Hello h = new Hello();
        h.sayInstanceHello();
    }
}
Hello from static!
Hello from instance!

When to Use Static Methods

Use static methods when:

Accessing Static Methods from Another Class

public class Greet {
    public static void sayHi() {
        System.out.println("Hi there!");
    }
}
public class Main {
    public static void main(String[] args) {
        Greet.sayHi();
    }
}
Hi there!

Calling Static Methods from Instance Methods

Static methods can be called from instance methods without any restriction:
public class Demo {
    public static void showStatic() {
        System.out.println("Static method called");
    }

    public void showInstance() {
        System.out.println("Calling from instance method...");
        showStatic(); // or Demo.showStatic();
    }

    public static void main(String[] args) {
        new Demo().showInstance();
    }
}
Calling from instance method...
Static method called

Limitations of Static Methods

Static methods have some boundaries:
public class Test {
    int x = 100;

    public static void display() {
        // System.out.println(x); // Compile-time error
        System.out.println("Static method can't access instance variables directly");
    }
}

Static Block vs Static Method

public class Example {
    static {
        System.out.println("Static block executed");
    }

    public static void greet() {
        System.out.println("Static method executed");
    }

    public static void main(String[] args) {
        Example.greet();
        Example.greet();
    }
}
Static block executed
Static method executed
Static method executed

Best Practices for Static Methods

Common Examples in Java API

These are all static — no need to instantiate the class.

Conclusion

Static methods are powerful and lightweight when used correctly. They let you organize functionality at the class level, simplify access to shared operations, and improve performance for tasks that don’t require object state. But they must be used purposefully — balance them with instance methods to stay true to object-oriented design. If you're writing utility logic, think static. If you're modeling behavior tied to an object, think instance.

QUIZ

Question 4:Which of the following statements correctly describes a static method in Java?

Question 5:Static methods in Java can access non-static member variables directly.

Question 6:Which of the following are valid characteristics of static methods in Java?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M