Java methods package behavior behind typed parameters and return values. Their contracts should make mutation, failure, overloading, static access, and recursion boundaries clear.
A Java method contract defines accepted arguments, returned results, mutation, failure, and whether dispatch is static, overloaded, recursive, or instance-based.
A method declaration includes access modifier, optional static, return type, method name, parameters, and body.
public class MethodBasics {
static int add(int a, int b) {
return a + b;
}
static void printLine(String text) {
System.out.println(text);
}
public static void main(String[] args) {
int total = add(10, 20);
printLine("Total: " + total);
}
}
Java passes arguments by value. For primitives, the copied value is passed. For objects, the copied reference is passed.
public class PassByValueDemo {
static void changeNumber(int n) {
n = 99;
}
public static void main(String[] args) {
int value = 10;
changeNumber(value);
System.out.println(value); // still 10
}
}
Overloading means multiple methods have the same name but different parameter lists. It improves readability when operations are conceptually the same.
public class OverloadingDemo {
static int area(int side) {
return side * side;
}
static int area(int length, int width) {
return length * width;
}
public static void main(String[] args) {
System.out.println(area(5));
System.out.println(area(4, 6));
}
}
A recursive method calls itself. Every recursive method needs a base case to stop and a recursive step that moves toward the base case.
public class RecursionDemo {
static int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
public static void main(String[] args) {
System.out.println(factorial(5));
}
}
A good method has one clear job, meaningful parameters, and a predictable return value. If a method becomes hard to name, it may be doing too many things.
A Java method is a named block of behavior. It may receive parameters, return a value, update object state, or perform an action. Good methods make code readable because the method name explains the intention and the body contains the steps. A method named calculateTax is easier to trust than repeating the same formula in five places.
Method signatures matter: the name, parameter list, and return type tell callers how to use the method. Overloading lets the same method name support different parameter lists, but it should still represent the same idea. Recursion is useful for problems that naturally shrink into smaller versions, but every recursive method needs a clear base case.
static double calculateFinalPrice(double price, double taxRate) {
return price + (price * taxRate);
}
double amount = calculateFinalPrice(500.0, 0.18);
System.out.println(amount);
Java passes every argument by value. For an object, that value is a copy of the reference.
No. Overloading requires a different parameter list.
Use it when the operation does not depend on instance state.
Explore 500+ free tutorials across 20+ languages and frameworks.