Methods
Methods group code into named, reusable actions. Good method names make programs read like plain English.
On this page · 5 min · Beginner
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 30, 2026
Related tutorials
In simple words#
A method is a vending machine button: you give inputs (coins and a choice), it does its work inside, and gives you something back (a snack).
- Parameter
- Input declared by the method.
- Return type
- Type of the value given back (void = nothing).
- Overloading
- Same name, different parameters.
Learning objectives#
- Declare methods with parameters and return values
- Call static methods
- Overload methods
- Understand pass-by-value
Explanation#
Syntax: modifiers returnType name(parameters) { body }. void means no return value.
static methods belong to the class (Math.max); instance methods belong to objects.
Overloading: several methods with the same name but different parameter lists. Varargs (int... values) accept any number of arguments.
Java is always pass-by-value: primitives are copied; for objects, the reference is copied (so the method can change the object’s contents, but not which object the caller’s variable points to).
Code example: Reusable price helpers#
public class Pricing {
static double withGst(double amount) {
return amount * 1.18;
}
static double withGst(double amount, double rate) { // overload
return amount * (1 + rate);
}
static int sum(int... values) { // varargs
int total = 0;
for (int v : values) total += v;
return total;
}
public static void main(String[] args) {
System.out.println(withGst(1000));
System.out.println(withGst(1000, 0.05));
System.out.println(sum(499, 1299, 250));
}
}
1180.0 1050.0 2048
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- withGst returns a calculated double.
- The overload accepts a custom rate.
- sum accepts any number of ints.
- main calls each method.
Real-world use case#
Pricing, tax and discount rules live in small methods that every part of a billing app reuses.
Common mistakes#
Split long methods; each should do one thing.
Java passes copies; return the new value instead.
Best practices#
- Verb names: calculateTotal, sendInvoice
- Few parameters
- Return values rather than printing inside
- Keep methods short
Remember this#
returnType name(params) { }
Overloading = same name, different params.
Java is always pass-by-value.
Practice exercise#
Your task: Write a method isAdult(int age) that returns true for 18 or above.
Show solution
static boolean isAdult(int age) {
return age >= 18;
}
Mini quiz#
What does void mean as a return type?
Want more? Practise all Java MCQs · Java interview questions
My notes#
Saved in this browser.