Java

Methods in Java

Write cleaner code with Methods! Understand definition, parameters, return values, and overloading.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Write cleaner code with Methods! Understand definition, parameters, return values, and overloading. This hands-on tutorial focuses on practical implementation of methods in java concepts.

Methods

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.

Why use methods? To reuse code: define the code once, and use it many times.

Method Declaration

public static int myMethod(int x) {
  return 5 + x;
}
  • public: Access modifier.
  • static: Method belongs to the Main class and not an object of the Main class.
  • int: Return type. void if it returns nothing.
  • myMethod: Name of the method.
  • int x: Parameter.

Calling a Method

public static void main(String[] args) {
  myMethod(3); // Prints nothing, but returns 8
  int result = myMethod(10);
  System.out.println(result); // Prints 15
}

Method Overloading

With method overloading, multiple methods can have the same name with different parameters.

int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)

[!NOTE] Multiple methods can have the same name as long as the number and/or type of parameters are different.

Scope

Variables declared inside a method are available only inside that method. This is called block scope.

Interactive Code

Create your own calculator using methods!

JAVA PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Java methods, return types, parameters and overloading"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 3

Which keyword indicates a method does not return a value?

null
void
empty
return

Next Steps

Methods use variables. But where do these variables live in the computer? Let's check out Memory Management.