Java Methods and Parameters Explained

A Java method names a reusable operation.

Lesson 27 of 127Beginner14 min read

A Java method names a reusable operation. Parameters declare the information that operation needs, and arguments supply actual values when you call it. A method can compute a value, change an object or perform an action such as printing. A clear method signature helps callers understand that purpose before reading the implementation.

Java always passes arguments by value. For primitives, the copied value is the primitive itself. For objects, the copied value is a reference. This does not make Java pass-by-reference; the difference explains why a method can change a referenced object but cannot replace the caller’s variable by reassigning its parameter.

Read a method declaration

A method declaration states modifiers, a return type, a name and a parenthesized parameter list, followed by a body. In static int add(int left, int right), static makes the method belong to the class, int declares the returned type, add names the operation and the two parameters declare its inputs.

A void return type means the method does not return a value to use in an expression. It may still print or modify state. Parentheses remain necessary even for a method with no parameters. Keep parameter names meaningful inside the method, but callers supply values rather than those names. A call such as add(4, 7) binds the first argument to left and the second to right in this signature.

Calling a method transfers control temporarily

When Java reaches a method call, it evaluates the argument expressions and passes their values to the selected method. The method’s statements run, and a return transfers control back to the caller. The caller can store or use a returned value. Printing inside a method is not the same as returning that value.

For example, a calculation helper that returns a total can support a console, web response or test. A helper that only prints the total ties the calculation to output. Neither is universally wrong, but separate calculation from presentation when you need reuse. In these examples, static helpers are called from static main without first constructing an object; instance methods are introduced with classes and objects.

Primitive parameters hold copies

If a caller passes an int variable to a method, the parameter receives that integer value. Reassigning the parameter changes only the method’s local variable. The caller’s int variable keeps its original value unless the caller assigns a returned result to it.

This is useful for reasoning about a helper: a method that increments its int parameter cannot secretly replace the caller’s count through that parameter. To communicate a new count, return it. Names do not create a connection between local variables in separate methods. A parameter named count and a caller variable also named count are distinct variables with separate scopes, even though the copied value initially matches.

Object references are copied too

When an argument is an object reference, the parameter receives a copy of that reference. Both copies can initially refer to the same object. Calling a mutating method through either reference changes the shared object. Assigning the parameter to a new object changes only the local reference and leaves the caller’s variable pointing to its original object.

For an array parameter, changing items[0] changes the existing array. Writing items = new int[]{9} does not replace the caller’s array reference. State explicitly whether your method mutates its input. A method name such as appendMarker suggests a change, while a name such as copyWithMarker suggests a new result. The signature alone does not guarantee whether mutation occurs.

Parameter types, scope and variable arguments

A parameter exists within its method body and is available for that invocation. Recursive or repeated calls create their own parameter values. Java checks argument compatibility during compilation, sometimes allowing numeric widening conversions. Supplying the wrong number of arguments normally fails unless an applicable overload or variable-argument method accepts them.

A varargs parameter such as int... values lets callers supply several ints, represented inside the method as an array. It must be the final parameter. Use it when a variable-sized collection of arguments fits the operation, not to avoid designing a clear signature. Start with ordinary fixed parameters and verify the relationship among inputs, calculation and return value before adding flexible calling forms.

Practice with explained solutions

Task 1. Write a method returning the sum of two int parameters and call it with 8 and 3.

Solution. Declare an int return type, return left + right, and print or store the returned 11 in the caller.

Task 2. Predict whether a method can change an array element received as an argument.

Solution. It can mutate the same array through the copied reference. Reassigning its parameter to another array does not replace the caller’s reference.

Java Summary

  • Java copies argument values, including object references.
  • A helper can mutate a shared object's state without replacing the caller's reference variable.
  • Return calculation results when callers need to reuse them independently of console output.
  • Continue with returns and overloading.

Java Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Return a reusable calculation

Save this complete program as RectangleArea.java. Compile with javac --release 17 RectangleArea.java and run with java RectangleArea.

Java
public class RectangleArea {
    static int area(int width, int height) {
        return width * height;
    }
    public static void main(String[] args) {
        int result = area(4, 6);
        System.out.println(result);
    }
}
Output
24
Line-by-line explanation
  1. Lines 1 to 9The call passes four as width and six as height. The helper multiplies them and returns twenty-four. main stores that returned value in result before printing it. The helper itself does not print, so another caller could reuse the result in a larger calculation. This example assumes nonnegative dimensions small enough to avoid int overflow.
Example 2

An int parameter does not replace the caller’s int

Save this complete program as PrimitiveParameter.java. Compile with javac --release 17 PrimitiveParameter.java and run with java PrimitiveParameter.

Java
public class PrimitiveParameter {
    static void increase(int value) {
        value++;
        System.out.println("Inside: " + value);
    }
    public static void main(String[] args) {
        int count = 5;
        increase(count);
        System.out.println("Outside: " + count);
    }
}
Output
Inside: 6
Outside: 5
Line-by-line explanation
  1. Lines 1 to 11increase receives a copy of five. Incrementing its parameter makes that local copy six, which the first output shows. After the call, count is still five. To update count, change the helper to return the increased value and assign count = increase(count) with a suitable int-returning signature.
Example 3

Mutation through a copied reference

Save this complete program as ArrayParameter.java. Compile with javac --release 17 ArrayParameter.java and run with java ArrayParameter.

Java
public class ArrayParameter {
    static void mark(int[] values) {
        values[0] = 9;
        values = new int[]{100};
    }
    public static void main(String[] args) {
        int[] original = {1, 2};
        mark(original);
        System.out.println(java.util.Arrays.toString(original));
    }
}
Output
[9, 2]
Line-by-line explanation
  1. Lines 1 to 11The parameter first points to the same array as original, so assigning its first element changes that shared array. Reassigning values afterward affects only the local parameter. original still points to the first array, now containing nine and two. This single program distinguishes object mutation from replacing a caller’s reference.

Common Java Mistakes

Saying Java passes objects by reference

Do not describe object parameters as passed by reference. That wording wrongly suggests a method can replace the caller’s variable through assignment to its parameter. Java copies the reference value. Also do not confuse a returned number with a printed number: a void method that prints 24 cannot initialize an int variable with that output.

Java FAQ

Are argument names required to match parameter names?

No. The caller supplies expressions in the declared order. Parameter names belong to the called method’s body.

Can a method accept no parameters?

Yes. Keep the empty parentheses in both declaration and call. It may obtain information from fields, but explicit inputs often make a calculation easier to test.

Can Java parameters have default values?

No. Java has no default parameter values. The usual alternatives are overloads, where a shorter version calls the longer one with a fixed value, a builder object for many optional settings, or passing an object whose fields have defaults.

How many parameters should a method have?

As few as the job needs. Methods with one to three parameters are easy to call correctly; with five or six, callers start mixing up the order of values of the same type. Group related values into a record or object, such as an Address instead of four separate strings.

What does final on a parameter do?

It stops the method from reassigning that parameter, which some teams use to make intent clear. It doesn't make the object it refers to unchangeable, and it has no effect on the caller. Parameters used inside lambdas must already be effectively final.

Java Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Multiple choiceeasy

What is count after increase(count) when it began at 5?

Multiple choicemedium

Why does mark change original[0] but not replace original?

Java Quiz

2 questions to check what you learned.

  1. How does Java pass arguments?

    • By value
    • By reference for every object
    • By parameter name
  2. Which declaration returns no value?

    • void show()
    • int show()
    • String show()