Table of Contents

    Pure Method in Java

    Pure Method

    A pure method is a method that adheres to the following principles:

    1. No Side Effects: A pure method does not alter any external state or variables. It only operates on its input parameters and returns a result based solely on those inputs.

    2. Deterministic: For the same set of input values, a pure method always produces the same output. The output is predictable and consistent.

    3. No External Dependencies: It does not rely on or affect any external state, class variables, or other methods. The behavior of a pure method is entirely contained within itself.

    Example:

    
    public int add(int a, int b) {
        return a + b; // Only depends on input parameters
    }
    
    
    • No Side Effects: The method does not modify any variables outside of its scope.
    • Deterministic: For the same values of a and b, the method will always return the same result.
    • No External Dependencies: It only depends on its parameters.