1. Static methods

In addition to static variables, classes can also have static methods.

Regular methods are bound to objects (instances) of a class and can refer to ordinary (non-static) variables of the class (as well as static variables and methods). Static methods are bound to the class's static object and can only access the static variables and/or other static methods of the class.

To call an ordinary (non-static) method on a class, you must first create an object of the class and then call the method on the object. You cannot call an ordinary method on the class rather than an object.

Example:

You cannot call a non-static method on a class!
public class DataInfo
{
   public int getValue()
   {
      return 100;
   }
}

public class Solution
{
   public static void main(String[] args)
   {
      System.out.println(DataInfo.getValue()); // This will generate an error!
   }
}

But to call a static method, it is enough for the class's static object to simply exist (and it always exists after the class is loaded into memory). This is why the main() method is static. It is bound to the class's static object, so you don't need to create any objects to call it.

To declare a method static, you need to write the static keyword before the method header. The general appearance of this construct is as follows:

static Type name(parameters)
{
   method code
}

Examples:

Code Note
public class Solution
{
   public static void main(String args[])
   {
      test();
   }

   public static void test()
   {
      int d = 2/0;
   }
}


The Java machine calls the main method with a command like this: Solution.main();



The static test() method is called in the static main() method.

To call a static method from another class, you need to specify the class name before the name of the static method. The general appearance of this construct is as follows:

Type name = ClassName.methodName(arguments)

Examples:

Code Static method
int x = Math.min(a, b);
int min(int a, int b)
Thread.sleep(200);
void sleep(long ms)
Path path = Path.of("c:\\readme.txt");
Path of(String str)


2. Static vs ordinary (non-static) methods

What is the difference between static methods and ordinary ones?

An ordinary method is bound to an ordinary object (an instance of a class), while a static method is not. An ordinary method can access variables in its instance, but a static method cannot: it simply doesn't have an associated instance.

The differences between the two types of methods are expressed in the following table:

Ability/property Ordinary method Static method
Bound to an instance of the class Yes No
Can call ordinary methods of the class Yes No
Can call static methods of the class Yes Yes
Can access ordinary variables of the class Yes No
Can access static variables of the class Yes Yes
Can be called on an object Yes Yes
Can be called on the class No Yes

Why are such methods needed if they are so severely limited? The answer is that this approach also has its advantages.

First, you don't need to pass any object reference in order to access static methods and variables.

Second, sometimes you need there to be just a single instance of a variable. For example, System.out (the System class's static out variable).

And, third, sometimes you need to call a method before it is even possible to create objects. For example, the Java machine calls the main() method to start the execution of the program even before an instance of the class is created.

Bound to an instance of the class

When an ordinary method is called, an argument — the object on which the method is called — is implicitly passed to it. This parameter is called this. This implicit parameter (a reference to the object on which the method is called) distinguishes ordinary methods from static ones.

Static methods do not have this implicit parameter, so you cannot use the this keyword inside static methods, and you cannot call a non-static method inside a static method. There is simply nowhere to get a reference to an instance of a class.

Can call ordinary methods of the class

An ordinary method always has the implicit this parameter, so you always have a reference to the object on which the method is called. Whenever you call an ordinary method inside another ordinary method, the implicit this parameter is used to make that call. Example

Code How it works
int min(int a, int b)
{
   return a < b ? a : b;
}

int min(int a, int b, int c)
{
   int t = min(a, b);
   return min(t, c);
}
int min(int a, int b)
{
   return a < b ? a : b;
}

int min(int a, int b, int c)
{
   int t = this.min(a, b);
   return this.min(t, c);
}

That is why you cannot call an ordinary method from a static one. There is simply no implicit variable named this inside a static method.

Or imagine another situation: not a single object of our class has yet been created in our program. Can we call a static method of our class? Yes. And can this static method call an ordinary (non-static) method?

Well, what object would we call it on? After all, not a single instance of our class exists yet!

Can call static methods of the class

Static methods can be called from anywhere — from any place in the program. This means that they can be called from both static methods and ordinary ones. There are no restrictions here.

Can access ordinary variables of the class

You can access ordinary variables of a class from an ordinary method, since it can easily obtain a reference to an instance of the class via the implicit this parameter.

A static method doesn't know which instance of the class it should use to get values of ordinary variables. And more generally, we can easily have a situation where a static method is called but not a single instance of the class has yet been created in the program.

As a result, static methods cannot access ordinary variables of a class.

Suppose a static method calls an ordinary method. What object should that ordinary method be called on?

Static methods

Nobody knows! That's why you cannot call an ordinary method from a static one without passing in a reference to an object!

Can access static variables of the class

The situation with calls to static variables is the same as with calls to static methods. Static variables can be accessed from anywhere in the program. That means that you can access them from static and ordinary methods.

Can be called on an object

Both static and ordinary methods can be called on an object. An ordinary method call possible — indeed, that is the only way to call an ordinary method. A static method can also be called on an object: in this case the compiler itself determines the type of the variable and calls the static method based on its type:

Code How the compiler sees it
Thread th = Thread.current();
th.sleep(1000);
Thread th = Thread.current();
Thread.sleep(1000);
Integer i = 1;
int x = i.parseInt("12");
Integer i = 1;
int x = Integer.parseInt("12");
"".valueOf(12);
String.valueOf(12);

Can be called on the class

You can call only a static method on a class. To call an ordinary method, you need a reference to an instance of the class. Therefore, you cannot call an ordinary method using this construct: ClassName.methodName(arguments)