How to Write and Call a static Method in Java
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
static <returnType> <name>(<type> <parameter>, <type> <parameter>) {
return <value>;
}
| Part | What it does |
|---|---|
static | Makes 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.javaruns 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:

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 twointvalues and returns anint.- 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
java Main.java
12
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
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out static and calling the method from main | error: non-static method area(int,int) cannot be referenced from a static context | Add static, or create an object first |
Declaring a return type but no return | error: missing return statement | Return 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: int | Pass one value per parameter, in order |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
non-static method … cannot be referenced from a static context | A static method called an instance method | Make the called method static, or call it on an object |
missing return statement | A path through the method ends without return | Add 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
- JLS 8.4.3.2: static Methods, in the Java Language Specification.
