Ch 8 · Methods · Java
Topic 8 of 13 in Java — Complete Tutorial (Beginner) — 3 lessons.
What is a Method?
A method is a named, reusable block of code that performs a task — Java's word for a function. Instead of repeating the same code, you write it once in a method and call it whenever needed (the DRY principle: "Don't Repeat Yourself"). In Java, a method declares its return type (the kind of value it gives back, or void for nothing), a name, and parameters in brackets. Methods inside our examples are marked static so main can call them directly.
Parameters and Return Values
Parameters are the typed inputs a method expects, listed in its brackets. When you call the method you pass matching values, called arguments. The return type, written before the method name, says what kind of value the method hands back; you use the return keyword to send it. A method that just performs an action and returns nothing has the return type void. Returning values lets you use a method's result in further calculations.
Method Overloading & Composition
Java lets you give several methods the same name as long as their parameters differ — this is called overloading, and Java picks the right one based on the arguments you pass. Methods can also call other methods, combining small focused pieces into a larger workflow (composition). The example shows one method building on others to print a report — the hallmark of well-organised code.