Java Static Method Tutorial

In this section, we will learn what the static method is and how to use it in Java.

Note: Before reading this section, we’re assuming you’re already familiar with static variables.

What is Static Methods in Java?

When we want to use a method of a class to run a task, usually we need to first create an object from that class and then we can use this object to access the method and then run the task.

But the problem here is that inside the object there might be multiple methods and member variables (attributes) that we really didn’t need them but they came up with the object. And now they’re wasting the memory space that could be used for other purposes!

For this reason, Java introduced static methods.

A method that is declared as `static` can be directly accessed via the name of that class and there’s no need to create an object from the class anymore!

How to declare a static method in Java?

This is how we can create a static method:

Access-specifier static data-type method-name(parameters…){…}

As you can see, we use `static` keyword between the `access-specifier` and the `data-type` of the method.

Notes to consider when creating static methods:

  • The memory of static methods is in a location called METASPACE.
  • Inside the body of static methods, we can’t use the `this` and `super` keywords because static methods are independent from objects.
  • We can’t override a static method.
  • Inside the body of a static method, we can’t access non-static methods or variables unless we use objects inside such methods to access those types of non-static methods and variables.
  • Inside the body of a static method, we can access static variables and static methods.

How to call a static method in java?

A static method, just like static variables, can be accessed directly via the name of the class from which the method is declared in.

Also, objects can access a static method, but usually it’s best practice to only access static methods via the name of their classes.

If we want to access a static method inside a non-static method, we need to use the name of the class before the name of the static method as well.

Calling a static method:

This is how we call a static method in Java:

ClassName.StaticMethodName();

First, start with the name of the owner class and then call the target static method using the dot operator.

Example: accessing static method inside static and non-static methods in Java

public class Simple {

    public static void main(String[] args) {
        System.out.println(Math.max(20,40));
        System.out.println(Math.min(50,100));
    }
}

Output

40

50

The `Math` class is one of those Java built-in classes that has many static methods.

As you can see, without the need to create an object from the `Math` class, we directly accessed two of these static methods and use them in the program.

Note: the `max()` method takes two input values and will return the number that is greater than the other. Also, the `min()` method takes two input values and it will return the number that is less than the other.

Is it possible to run a program without the `main()` method?

No! the main method is the entry point in Java programs.

Java Static Method vs Instance Method

In short, instance methods are copied for each object that is created from the target class and so each object will have its own copy of the method.

On the other hand, static methods are unique! Which means there’s only one copy of them in the target class and all the objects will share this method.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies