๐ŸŽฏ 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
๐Ÿ’ผ Interview reality: "What is method overloading vs overriding?" is asked in virtually every Java round. Overloading is compile-time; overriding is runtime. This lesson covers overloading โ€” overriding comes in Arc B (OOP).

๐Ÿ› ๏ธ 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.

javac Methods.java && java Methods
Methods.javaLesson 05 ยท overloaded interest methods
verified output
24-month interest: โ‚น38000.00 12-month interest: โ‚น19000.00

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.

javac Scope.java && java Scope
Scope.javaLesson 05 ยท local vs static scope
verified output
Payment #1: gross=โ‚น5000.00 fee=โ‚น75.00 net=โ‚น4925.00 Payment #2: gross=โ‚น12000.00 fee=โ‚น180.00 net=โ‚น11820.00 Payment #3: gross=โ‚น750.00 fee=โ‚น11.25 net=โ‚น738.75 Total payments processed: 3

๐Ÿฆ Real-world scenario

๐Ÿฆ
Real-world scenario ยท SouthBank India
Arjun writes a varargs deposit aggregator

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.

javac VarArgs.java && java VarArgs
VarArgs.javaSouthBank India ยท varargs deposit aggregator
verified output
1 deposit : โ‚น5000.00 3 deposits : โ‚น20500.00 0 deposits : โ‚น0.00

๐Ÿ“ฆ 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 passWhat the method getsCan method change caller's variable?
Primitive (int, double)Copy of the valueNo
Object reference (StringBuilder, array)Copy of the referenceCan mutate the object; cannot rebind the reference
StringCopy of the referenceNo โ€” String is immutable; reassignment creates a new object

๐Ÿงฉ Mini challenge

Three method exercises
Task 1 โ€” EMI calculator method

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.

Task 2 โ€” Overload isEligible

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.

Task 3 โ€” Pass-by-value demo

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

EasyWhat is method overloading in Java?
+

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.

EasyWhat does static mean on a method?
+

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.

MediumIs Java pass-by-value or pass-by-reference?
+

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).

MediumWhat are varargs and what restrictions apply to them?
+

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).

HardWhat is the difference between overloading and overriding, and which is resolved at compile time vs runtime?
+

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

Question 1 of 5
What distinguishes two overloaded methods in Java?
Question 2 of 5
Where must a varargs parameter appear in a method signature?
Question 3 of 5
If you pass an int to a method and the method doubles it, what happens to the caller's variable?
Question 4 of 5
A local variable declared inside a method is accessible:
Question 5 of 5
Overloading is resolved at:
0
/ 5
Quiz complete!

Next: arrays โ€” 1D and 2D โ†’

โš ๏ธ Common mistakes

Overloading only by return type
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.
Expecting primitives to change after method call
Passing an 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.
Using a local variable outside its block
Declaring 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.
Varargs position error
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.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’