Java Tutorial: Methods
Define and call methods, parameters, return types, method overloading, varargs, recursion, and static vs instance methods.
Don't repeat yourself
Methods let you name a piece of logic and reuse it anywhere. Write once, call everywhere. Change it in one place and it updates everywhere it's used.
Java methods are inside classes — always. The `static` keyword means they belong to the class itself, not to an instance. You'll understand the non-static case deeply in Chapter 9.
What you'll learn in this Java methods tutorial
This interactive Java tutorial has 7 hands-on exercises. Estimated time: 20 minutes.
- Your first method — A method has a return type, a name, parameters, and a body:
- Parameters and return values — Methods can take multiple parameters and return any type.
- Method overloading — Overloading: multiple methods with the **same name** but different parameters.
- Varargs — variable argument count — With `...` (varargs), a method accepts any number of arguments as an array:
- Early return — Return as soon as you have the answer — no need to reach the end:
- Recursion — methods calling themselves — A method that calls itself is recursive. Every recursive method needs:
- Build a string utilities class — Final challenge — no starter code.
Java Methods concepts covered
- Don't repeat yourself