Java Methods: Declaration, Pass-By-Value & Method Overloading

A method is a reusable block of organized statements designed to execute a specific task, manipulate object state, and return a computed result. Methods promote the DRY (Don't Repeat Yourself) principle and enhance software modularity.

Anatomy of a Method Declaration

// Method Syntax:
// [access_modifier] [static] return_type methodName(parameter_list) { ... }

public static int calculateSum(int num1, int num2) {
    int total = num1 + num2;
    return total; // Must match declared return_type
}

Java is Strictly Pass-By-Value (The Golden Rule)

A frequent topic of confusion in technical interviews and BCA exams is whether Java passes arguments by value or reference. Java is 100% Pass-By-Value in all circumstances:

  • Primitive arguments: A copy of the actual binary value is passed. Modifying the parameter inside the method has zero effect on the caller's variable.
  • Object reference arguments: A copy of the reference address pointer is passed. Through this copied pointer, the method can mutate the internal state of the referenced object. However, reassigning the parameter to a new object does NOT redirect the caller's variable!
public class PassByValueDemo {
    public static void modifyPrimitive(int x) {
        x = 999; // Modifies local copy only!
    }

    public static void modifyArray(int[] arr) {
        arr[0] = 500; // Modifies shared Heap array object!
    }

    public static void main(String[] args) {
        int a = 10;
        modifyPrimitive(a);
        System.out.println("a after method: " + a); // Still 10!

        int[] scores = { 10, 20, 30 };
        modifyArray(scores);
        System.out.println("scores[0] after: " + scores[0]); // Changed to 500!
    }
}

Method Overloading (Compile-Time Polymorphism)

Method Overloading occurs when multiple methods in the same class share the exact same method name, but possess different parameter lists:

  • Different number of parameters: add(int, int) vs add(int, int, int)
  • Different data types of parameters: print(int) vs print(String)
  • Different sequence/order of types: display(int, String) vs display(String, int)
public class Calculator {
    // 1. Two integer parameters
    public int add(int a, int b) {
        return a + b;
    }

    // 2. Overloaded: Three integer parameters
    public int add(int a, int b, int c) {
        return a + b + c;
    }

    // 3. Overloaded: Two double parameters
    public double add(double a, double b) {
        return a + b;
    }

    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(10, 20));       // Invokes #1 -> 30
        System.out.println(calc.add(10, 20, 30));   // Invokes #2 -> 60
        System.out.println(calc.add(5.5, 4.5));     // Invokes #3 -> 10.0
    }
}

University Exam Corner (BCA / B.Sc CS)

Exam Question: "Can we overload the main method in Java?"

Answer: Yes! You can have multiple overloaded main methods with different parameters (e.g. main(int a), main(String a, String b)). However, the JVM launcher will ONLY invoke the standardized entry point signature public static void main(String[] args). The other overloaded main methods must be called explicitly by your code.

Next Step

Congratulations on mastering Java fundamentals! Let us enter Section 2 and explore control flow, decision branching, and loops. Proceed to: Java If..Else Conditions →

Frequently Asked Questions (FAQ)

Q1. Is Java pass-by-value or pass-by-reference?
Java is strictly 100% pass-by-value! When you pass an argument to a method, a copy of the primitive value or a copy of the object reference address is passed into the parameter. You cannot reassign the original caller's variable pointer to a new object.
Q2. What constitutes a Method Signature in Java?
A method signature consists strictly of the method name and the ordered list of parameter data types. The return type and access modifiers are NOT part of the method signature!
Q3. Can two methods be overloaded solely by changing their return types?
No! In Java, method overloading requires a difference in parameter lists (different number of arguments, different data types, or different argument ordering). Changing only the return type causes a compilation error: 'method already defined'.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java Syntax Rules →

Method anatomy.

Polymorphism (Overriding vs Overloading) →

Compile-time vs runtime dispatch.

Java Constructors →

Constructor overloading.

Recursion in Java →

Methods calling themselves.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!