0
0
JavaHow-ToBeginner · 3 min read

How to Use Static Method in Java: Syntax and Examples

In Java, a static method belongs to the class rather than an instance, so you call it using the class name. You define it with the static keyword before the return type, and you can call it without creating an object.
📐

Syntax

A static method is declared with the static keyword before the return type. It belongs to the class, not to any object instance.

  • static: keyword to declare the method as static
  • returnType: the type of value the method returns (use void if it returns nothing)
  • methodName: the name of the method
  • parameters: input values inside parentheses

You call a static method using the class name, like ClassName.methodName().

java
public class MyClass {
    public static void myStaticMethod() {
        System.out.println("Hello from static method!");
    }
}
💻

Example

This example shows how to define a static method and call it from the main method without creating an object.

java
public class StaticExample {
    public static void greet() {
        System.out.println("Welcome to static methods in Java!");
    }

    public static void main(String[] args) {
        StaticExample.greet(); // Call static method using class name
    }
}
Output
Welcome to static methods in Java!
⚠️

Common Pitfalls

Common mistakes when using static methods include:

  • Trying to call a static method using an object reference instead of the class name (though allowed, it is discouraged).
  • Accessing instance variables or methods directly inside a static method (not allowed because static methods don’t belong to an instance).
  • Forgetting the static keyword when defining the method.
java
public class PitfallExample {
    int number = 5;

    // Wrong: static method trying to access instance variable directly
    public static void wrongMethod() {
        // System.out.println(number); // This causes a compile error
    }

    // Correct: pass instance data as parameter
    public static void correctMethod(int num) {
        System.out.println(num);
    }

    public static void main(String[] args) {
        PitfallExample obj = new PitfallExample();
        // Calling static method correctly
        PitfallExample.correctMethod(obj.number);
    }
}
Output
5
📊

Quick Reference

ConceptDescription
static keywordDeclares a method belongs to the class, not instances
Calling static methodUse ClassName.methodName() without creating an object
Access restrictionsStatic methods cannot access instance variables or methods directly
Use caseUtility or helper methods that don’t need object data
Main methodpublic static void main(String[] args) is static to run without objects

Key Takeaways

Use the static keyword to define methods that belong to the class itself.
Call static methods using the class name, not an object instance.
Static methods cannot directly access instance variables or methods.
Static methods are useful for utility functions and the main entry point.
Avoid calling static methods through objects to keep code clear.