View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Static Method in Java: Everything You Need to Know

Updated on 24/04/20253,875 Views

In Java, the static keyword often confuses beginners—especially when it comes to static methods. Think of a static method like a TV remote: you don’t need a specific TV (object) to understand how the remote (method) works; it's designed to control any compatible TV. Similarly, a static method belongs to the class itself, not to any instance. 

In this blog, we’ll explore static methods in Java, how it works, when to use it, and clear up common doubts like “can we override static method in Java?” or “why is the main method static in Java?” Let’s understand!

Enhance your Java skills with expert-led training. Check out upGrad’s Software Engineering course and learn how to apply Java in real-world projects.

What is a Static Keyword in Java?

In Java programming language, the static keyword means the member (variable, method, block, or nested class) belongs to the class itself, not to any object created from it. This allows access without creating an instance of the class. 

Static members are shared among all objects, saving memory and improving performance. Common uses include utility methods (like Math.sqrt()) and constants. The main() method is also static because it runs without creating an object.

Take your Java development career to the cloud. Learn CI/CD, Kubernetes, and more with upGrad’s Cloud Computing and DevOps certificate program.

Introduction to Static Method in Java

A static method in Java is a method that belongs to the class rather than any object. It can be invoked directly using the class name and doesn't require an object of the class to be created. It’s commonly used for utility or helper methods.

Example:

public class Utility {
    static void greet() {
        System.out.println("Hello, Java Developer!");
    }

    public static void main(String[] args) {
        Utility.greet();  // Calling static method without object
    }
}

Output:

Hello, Java Developer!

Explanation:

Here, the greet() method is static, so it can be accessed using the class name without creating an object.

Take your Java knowledge to the next level. Explore the Executive Diploma in Data Science & AI by IIIT-B to dive into machine learning, AI, and analytics.

Applications of Static Method in Java

1. Static Block in Java

A static block is a code block that runs once when the class is loaded into memory, even before the main method. It is mainly used to initialize static variables or perform setup operations. Static blocks are executed in the order they appear in the class.

Example:

public 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.

Explanation: The static block runs automatically when the class is loaded, even before the main() method is executed. It’s a great place for initializing static data or logging when a class is first accessed.

2. Static Variable in Java

A static variable is shared across all instances of a class. It belongs to the class itself rather than any object, so all objects refer to the same memory location. It's often used for common values like counters, constants, or configuration values that should be shared.

Example Code:

public class StaticVariableExample {
    static int count = 0;

    StaticVariableExample() {
        count++;
        System.out.println("Object count: " + count);
    }

    public static void main(String[] args) {
        new StaticVariableExample();
        new StaticVariableExample();
    }
}

Output:

Object count: 1
Object count: 2

Explanation:Here, the count variable is static, so its value is shared among all objects. Each time a new object is created, the same count is incremented and printed, reflecting the total number of objects created.

Static Method in Java

A static method in Java belongs to the class and not any object. It can be called using the class name and can only access static variables or call other static methods directly. It’s useful for utility functions or operations that don’t depend on instance-specific data.

Example Code:

public class StaticMethodExample {
    static void greet() {
        System.out.println("Hello from static method!");
    }

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

Output:

Hello from static method!

Explanation:The greet() method is static, so it can be called without creating an object. It's invoked using the class name, showing that the method belongs to the class, not any instance.

When to Use Static Variables and Methods?

Use static variables when a value should be shared among all class instances, like counters or constants. Use static methods when the behavior doesn't rely on instance variables. This improves memory efficiency and allows direct access via the class name, making them ideal for utility or helper functions.

Example:

public class Utility {
    static int square(int n) {
        return n * n;
    }

    public static void main(String[] args) {
        System.out.println("Square of 5: " + Utility.square(5));
    }
}

Output:

Square of 5: 25

Explanation:

The static method square() performs a calculation without relying on any object data. It's accessed directly through the class, making it a good fit for stateless utility operations.

Static Classes in Java

Explanation: In Java, only nested classes can be declared static. A static nested class can access static members of the outer class but not non-static ones. Static nested classes are often used for grouping helper classes logically without needing an object of the outer class.

Example:


public class Outer {
    static int data = 30;

    static class Inner {
        void show() {
            System.out.println("Data: " + data);
        }
    }

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

Output:

Data: 30

Explanation:The inner class is static, so it can be accessed without creating an object of the outer class. It accesses the static variable data from the outer class, which shows the purpose of a static nested class.

Can We Override Static Method in Java?

No, static methods cannot be overridden because they are bound to the class, not to objects. If you define a static method in the subclass with the same name, it hides the parent method (method hiding), not override.

Example:

class Parent {
    static void show() {
        System.out.println("Parent static method");
    }

    public static void main(String[] args) {
        Parent.show(); // Output: Parent static method
        Child.show();  // Output: Child static method
    }
}

class Child extends Parent {
    static void show() {
        System.out.println("Child static method");
    }
}

Output:

Parent static method  
Child static method

Explanation:

Static methods are not overridden; they are hidden when redefined in a subclass. In this code, calling show() on each class separately executes the version defined in that class, showcasing that static methods are class-bound, not object-bound.

Also read: Overloading vs Overriding in Java

Why is Main Method Static in Java?

The main method is static because it allows the JVM to call it without creating an instance of the class. Since execution starts from main(), it must be accessible even before any objects are created.

Example:

public class MainExample {
    public static void main(String[] args) {
        System.out.println("Program started without creating an object.");
    }
}

Output:

Program started without creating an object.

Explanation:

The main method runs first when a Java program starts. Declaring it static allows the JVM to invoke it directly using the class name—without needing an object—making it the official program entry point.

Conclusion

Understanding how and when to use the static method in Java is crucial for writing efficient and memory-optimized code. The static keyword is foundational in Java programming, from the main method to utility classes. Whether you're wondering "can we override the static method in Java" or "why is the main method static in Java," we hope this guide clarifies everything.

Top FAQs on Static Method in Java

What is a static method in Java? 

A static method belongs to the class rather than any instance of the class. It can be invoked using the class name, without needing an object. Static methods are typically used for utility or helper functions that do not depend on instance variables.

Can we override a static method in Java? 

No, static methods cannot be overridden. They are resolved at compile time, and method calls are bound to the class type, not the instance type. While method hiding can occur in inheritance, it isn't true overriding.

Why is the main method static in Java?

The main method is static because it allows the JVM to invoke it without creating an instance of the class. This is crucial for the program’s execution since the main method serves as the entry point to the application.

Can static methods access instance variables in Java? 

No, static methods cannot directly access instance variables or instance methods. They can only access static variables and methods. To access instance variables, a static method would need to use an object of the class.

What is method overloading with static methods in Java? 

Static methods can be overloaded, meaning multiple static methods with the same name but different parameter lists can exist in a class. Overloading is determined by the method signature, allowing different behaviors based on input.

Can we call a static method from an instance method in Java? 

Yes, static methods can be called from instance methods, though it is recommended to use the class name for clarity. Static methods are accessible to instance methods as they belong to the class and not an instance.

What is the use of static methods in Java? 

Static methods are commonly used for utility functions that do not require object states. They can also be used for tasks like managing shared resources or providing global access points to methods, such as in singleton patterns.

Can a static method access the this keyword in Java? 

No, a static method cannot use the this keyword because it does not belong to any specific instance of the class. The this keyword refers to the current object, but static methods do not have access to object-level context.

How do static methods behave in inheritance in Java? 

Static methods are inherited but are not overridden. If a static method in a subclass has the same signature as a static method in the parent class, it hides the parent method. This is known as method hiding, not overriding.

Can we declare a constructor as static in Java? 

No, constructors cannot be static in Java. Constructors are used to create instances of a class, and a static method cannot operate on a specific instance. Declaring a constructor as static would contradict its purpose.

How are static methods stored in memory in Java?

Static methods are stored in the method area of the JVM memory, which is shared by all instances of the class. Since they belong to the class rather than individual objects, static methods are loaded into memory once when the class is loaded.

image

Take the Free Quiz on Java

Answer quick questions and assess your Java knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.