Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Methods and objects

Last updated: 28 Sept 20264 min read
guide
By AITrove Editorial

A Java method defines a named operation with parameter types, a return type, and a body; callers pass argument values into a new invocation.

Define what the operation promises

A calculation should state its input range, units, failure behavior, and whether it changes caller-owned state. A method named totalCents that accepts an array should not silently store that array for unrelated later work.

The batch below uses positive and zero invoice line values, rejects negative values, and accumulates as long. Math.addExact makes long overflow a failure rather than wrapping into a plausible total. This is a local calculation, not a payment operation.

Parameter values are copied into each invocation. Copying an array reference does not copy its contents. The method only reads the array; see pass-by-value and shared references before adding mutation.

Overloading is a compile-time choice

Overloaded methods share a name but have different parameter lists. Return type alone cannot distinguish two overloads with the same parameters. The compiler chooses an applicable signature from the declared argument types; runtime instance overriding is a separate dispatch rule.

Varargs allows a variable-length argument list by presenting an array parameter inside the method. It can introduce an array allocation at a call site. Passing an existing compatible array supplies that array reference rather than automatically creating a detached copy.

Working program

Java
public class InvoiceMethodContract {
    static long totalCents(int[] lineAmounts) {
        if (lineAmounts == null) throw new IllegalArgumentException("Missing amounts");
        long total = 0L;
        for (int amount : lineAmounts) {
            if (amount < 0) throw new IllegalArgumentException("Negative line amount");
            total = Math.addExact(total, amount);
        }
        return total;
    }
    public static void main(String[] args) {
        System.out.println(totalCents(new int[] {1250, 0, 750}));
        System.out.println(totalCents(new int[0]));
    }
}

Output

Output
2000
0

Cost and design choices

The operation visits n line amounts in O(n) time and uses O(1) extra local state. The supplied array is not cloned. A new method frame is not equivalent to a new copy of every referenced object.

A small method can still be expensive if it performs remote work or repeatedly traverses a collection. Method length is not a performance measure. Keep side effects visible in the contract and test empty, invalid, and boundary inputs.

Common Mistakes

  • Do not overload only by return type.
  • Do not describe a passed reference as a copied object.
  • Do not silently return zero for invalid input when zero is also a valid result.
  • Do not ignore integer range when choosing an accumulator.

Connect the contracts

Compare the boundary explained in Java Collections with the assumptions made by this program.

java
Storage details