๐ฏ What you will learn
So far every line of code lives in main(). That works for 10 lines. A real Spring Boot service has thousands. Methods are how you name, reuse, and test units of logic. By the end of this lesson you will understand:
- Method signature: return type, name, parameter list
- Method overloading โ same name, different parameters
- Varargs โ accept any number of arguments
- Scope โ where variables are visible and when they are destroyed
- Pass-by-value โ why primitives and references behave differently
๐ ๏ธ Method signatures and overloading
A method signature is returnType methodName(paramType param, ...). Overloading means defining multiple methods with the same name but different parameter lists โ the compiler picks the right one based on the arguments you pass.
Overloading rules: The compiler distinguishes overloads by the number and types of parameters โ not by return type. You cannot overload solely by changing the return type.
๐ญ Scope โ where variables live
A variable's scope is the block { } where it is declared. Local variables (inside a method) live only for that method call. Class-level static fields live for the program's lifetime.
๐ฆ Real-world scenario
Arjun needs to compute the total of any number of deposits in a single call โ sometimes one, sometimes five. Rather than overloading for every count, he uses varargs (double...), which lets the caller pass any number of arguments and Java collects them into an array.
๐ฆ Pass-by-value in Java
Java is always pass-by-value. For primitives, a copy of the value is passed โ changing the parameter inside the method doesn't affect the caller's variable. For objects, a copy of the reference is passed โ you can mutate the object's fields, but you can't make the caller's variable point to a different object.
| What you pass | What the method gets | Can method change caller's variable? |
|---|---|---|
Primitive (int, double) | Copy of the value | No |
Object reference (StringBuilder, array) | Copy of the reference | Can mutate the object; cannot rebind the reference |
String | Copy of the reference | No โ String is immutable; reassignment creates a new object |
๐งฉ Mini challenge
Write a static method computeEmi(double principal, double annualRate, int months) that returns the monthly EMI using the standard formula. Call it with โน5,00,000 at 10% for 60 months. Print the EMI.
Write two overloaded methods: isEligible(int creditScore) that returns true if score โฅ 700, and isEligible(int creditScore, double income) that also requires income โฅ 30000. Print both results for the same score.
Write a method doubleIt(int n) that does n *= 2. Call it, then print n in the caller. Observe that the caller's value is unchanged. Then do the same with an int[] โ observe that the array element IS changed.
๐ค Interview Q&A
Method overloading means defining multiple methods in the same class with the same name but different parameter lists (different number, types, or order of parameters). The compiler selects the correct method at compile time based on the argument types โ this is called static dispatch or compile-time polymorphism. Return type alone does not differentiate overloads โ trying to overload only by return type is a compile error.
A static method belongs to the class, not to a specific instance. You call it with ClassName.methodName() and it has no implicit this reference. It cannot directly access instance fields or instance methods โ only other static members. In Arc A we use static methods because we have not introduced objects yet. In Spring Boot services, static utility methods (validation, formatting, math) are common; business logic goes in instance methods on @Service beans.
Java is always pass-by-value. For primitives, the actual value is copied. For object references, the reference (memory address) is copied โ so you can mutate the object's fields through the copy, but you cannot make the caller's reference variable point to a different object. This is why people sometimes say "pass-by-reference for objects" โ they're describing the observable effect (mutations are visible to the caller), not the mechanism (the reference itself is copied).
Varargs (Type... name) let a method accept zero or more arguments of the given type. The compiler bundles them into an array. Restrictions: (1) a method can have at most one varargs parameter; (2) it must be the last parameter; (3) you can also pass an explicit array. Varargs are syntactic sugar โ totalDeposits(5000, 12000) is identical to totalDeposits(new double[]{5000, 12000}). They appear frequently in Java's standard library: String.format(String fmt, Object... args).
Overloading (this lesson): same class, same method name, different parameter lists. Resolved at compile time โ the compiler picks the most specific matching signature. Also called static/compile-time polymorphism. Overriding (Arc B): a subclass provides a new implementation for an inherited method with the same signature. Resolved at runtime โ the JVM calls the actual object's type's method, not the declared reference type's. Also called dynamic/runtime polymorphism. Interview mnemonic: overloading is about the signature, overriding is about the object.
๐ Quiz โ 5 questions
int to a method and the method doubles it, what happens to the caller's variable?Next: arrays โ 1D and 2D โ
โ ๏ธ Common mistakes
int compute(double x) and double compute(double x) is a compile error. Return type alone cannot distinguish overloads โ the compiler uses only parameter types to resolve them.int to a method and expecting the caller's variable to change is the #1 pass-by-value mistake. The method gets a copy. To "return" a new value, use the method's return value explicitly.int x inside an if block and trying to use it after the closing brace is a compile error. Declare variables in the smallest scope that makes them available where needed.void foo(double... amounts, String label) fails โ varargs must be last. void foo(String label, double... amounts) is correct. Also: you cannot have two varargs parameters in one method.โ Key takeaways
- A method signature is
returnType name(paramType param, ...)โ methods with the same name but different parameters are overloaded. - Overloading is resolved at compile time โ the compiler picks the best matching signature.
- Varargs (
Type... name) accept any number of arguments and must be the last parameter. - Java is always pass-by-value โ primitives are copied; reference copies let you mutate objects but not rebind the caller's variable.
- Local variables live only inside the block that declares them; static fields live for the class lifetime.
- Methods with a single responsibility are easier to test, name, and reuse โ the foundation of clean Spring Boot service layers.
From methods to production Spring Boot
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ