Pure Method in Java
☰Fullscreen
Table of Content:
Pure Method
A pure method is a method that adheres to the following principles:
-
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.
-
Deterministic: For the same set of input values, a pure method always produces the same output. The output is predictable and consistent.
-
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
andb
, the method will always return the same result. - No External Dependencies: It only depends on its parameters.