Learn Java
import com.learnjava.methods.MethodsBasics;

Defining and Calling Methods in Java

@Concept

A method is a named, reusable block of code. Its signature declares an access modifier, a return type, a name, and a parameter list in parentheses, and its body, in braces, holds the actual statements that run each time it's called. Defining a method doesn't run it — calling it by name, with matching arguments, is what actually executes its body, and control returns to wherever it was called from once the method finishes.

Static methods belong to the class itself and can be called without creating an object first (main is the most obvious example, called automatically by the JVM), while instance methods belong to a specific object and need one to exist before they can be called — a distinction covered more fully once objects and classes are introduced properly.

@HowItWorks
1

The method signature

Access modifier, return type, name, and parameter list together describe what a method needs and returns.

2

The method body

The actual statements inside the braces, which only run when the method is called.

3

Calling a method

Writing its name with parentheses, and any required arguments, transfers control into its body.

4

Returning to the caller

Once a method's body finishes, or hits a return, execution resumes right after wherever it was called from.

5

static versus instance

A static method belongs to the class and needs no object to call; an instance method belongs to a specific object and needs one to exist first.

@Example
public class MethodDemo {
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
 
public static void main(String[] args) {
greet("Ada");
greet("Bo");
}
}
Output
Hello, Ada!
Hello, Bo!
1greet is defined once, above main, but its body doesn't run yet — defining it just makes it available to call.
2greet("Ada") transfers control into the method's body, with name bound to "Ada" for this call.
3Once that call's println finishes, control returns to main, right after the first greet call.
4greet("Bo") repeats the same process with a different argument, giving a second line of output.
@UseCases
Breaking a large program into smaller, named, reusable pieces
Calling the same logic repeatedly with different arguments instead of duplicating code
Understanding why main needs to be static, since the JVM calls it with no object to call it on
Reading an unfamiliar class by scanning its method signatures first

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.