Java Programming · Lesson 6 of 15 · Updated ·
Methods in Java — Parameters, Return, Overloading and Pass-by-Value
A method is a named block of code that does one job. In Java every method lives inside a class.
1. Defining and calling
public class Main {
static int square(int n) { // return type int, one parameter
return n * n;
}
static void greet(String name) { // void = returns nothing
System.out.println("Hello " + name);
}
public static void main(String[] args) {
greet("Asha");
int s = square(7);
System.out.println(s + square(2));
}
}
Hello Asha
53
Parts: static (call without an object), return type, name, parameters, body with return.
2. Method overloading
Same name, different parameter lists (number or types). Java picks the right one at compile time.
public class Main {
static int add(int a, int b) { return a + b; }
static int add(int a, int b, int c) { return a + b + c; }
static double add(double a, double b) { return a + b; }
public static void main(String[] args) {
System.out.println(add(2, 3));
System.out.println(add(2, 3, 4));
System.out.println(add(2.5, 1.5));
}
}
5
9
4.0
Changing only the return type is not overloading — it's a compile error.
3. Varargs: any number of arguments
public class Main {
static int sum(int... nums) { // nums is an int[]
int total = 0;
for (int n : nums) total += n;
return total;
}
public static void main(String[] args) {
System.out.println(sum() + " " + sum(5) + " " + sum(1, 2, 3, 4));
}
}
0 5 10
4. Java is always pass-by-value
Java copies the value of each argument into the parameter.
- For primitives, that's the number itself — the caller's variable can't change.
- For objects, the value copied is the reference. The method can change the object's contents, but can't make the caller's variable point to a new object.
import java.util.Arrays;
public class Main {
static void change(int x, int[] arr) {
x = 100; // only the copy changes
arr[0] = 100; // the shared array changes
arr = new int[]{7, 7}; // the copy now points elsewhere; caller unaffected
}
public static void main(String[] args) {
int n = 1;
int[] a = {1, 2};
change(n, a);
System.out.println(n + " " + Arrays.toString(a));
}
}
1 [100, 2]
This is one of the most asked Java interview questions: "Is Java pass-by-reference?" → No, always pass-by-value; for objects the value is a reference.
5. Recursion
public class Main {
static long factorial(int n) {
if (n <= 1) return 1; // base case
return n * factorial(n - 1);
}
static int power(int base, int exp) {
if (exp == 0) return 1;
return base * power(base, exp - 1);
}
public static void main(String[] args) {
System.out.println(factorial(10) + " " + power(2, 10));
}
}
3628800 1024
Each call gets its own stack frame; too deep recursion causes StackOverflowError.
Common mistakes
- Forgetting
returnin a non-void method (compile error). - Expecting a method to change a primitive argument.
- Overloading by return type only.
Quick recap
static returnType name(params) { … return value; }- Overloading = same name, different parameters.
type... namefor varargs.- Pass-by-value always; object references are copied, so contents can change.