Skip to main content

How to Write and Call a static Method in Java

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A method gives a name to a piece of work so you can run it with different values. A static method belongs to the class itself, not to an object, so you can call it straight away, which is exactly what main needs.

Syntax​

Syntax
static <returnType> <name>(<type> <parameter>, <type> <parameter>) {
return <value>;
}
PartWhat it does
staticMakes the method belong to the class, so it runs without an object.
<returnType>The type of value the method returns, or void for none.
(<type> <parameter>, ...)The values the method receives, each with its type.
return <value>Sends the result back to the caller.

The Java Language Specification, section 8.4.3.2: "A method that is declared static is called a class method. A class method is always invoked without reference to a particular object."

The Example​

The example writes a static area method that multiplies a width by a height, and calls it twice from main.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Method​

Create a file named Main.java:

Main.java, which declares static int area(int width, int height) returning width times height, and prints area(3, 4) and Main.area(10, 2) from main.

Main.java
public class Main {
static int area(int width, int height) {
return width * height;
}

public static void main(String[] args) {
System.out.println(area(3, 4));
System.out.println(Main.area(10, 2));
}
}
  • static int area(int width, int height) takes two int values and returns an int.
  • Inside the same class, area(3, 4) calls it by name.
  • Main.area(10, 2) calls it through the class, which is how code in other classes calls it.

Step 2: Run It​

Terminal
java Main.java
Output
12
20

A zsh terminal where java Main.java prints 12 and 20.

The command is the same in zsh, bash and PowerShell.

Why main Needs static Methods​

main is static, so it runs without any object of the class. From there it can call other static methods directly. A method without static belongs to an object, so main would first have to create one with new Main().

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out static and calling the method from mainerror: non-static method area(int,int) cannot be referenced from a static contextAdd static, or create an object first
Declaring a return type but no returnerror: missing return statementReturn a value on every path
Passing the wrong number of values, as in area(3)error: method area in class Main cannot be applied to given types; with required: int,int and found: intPass one value per parameter, in order

Troubleshooting​

SymptomLikely causeFix
non-static method … cannot be referenced from a static contextA static method called an instance methodMake the called method static, or call it on an object
missing return statementA path through the method ends without returnAdd the missing return

FAQ​

When should a method not be static?​

When it works with an object's own data, such as a BankAccount whose deposit method changes that account's balance.

Can static methods use variables of the class?​

Only static ones. Instance variables belong to objects, and a static method has no object.

Conclusion​

A static method belongs to its class, so main can call it directly by name, and other classes call it as ClassName.method(...). Give it a return type and return the result.

Additional Resources​