2

I know that A method is referentially transparent if its return value (with identical actual parameters) is independent of the program context.

e.g. this code is not transparent.

public static int Erna () {
  return y;
}

but can someone please give an example of java code which is transparent so I can understand this better?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332

1 Answers1

3
public static int addOne(int i) {
    return i + 1;
}

is a referentially transparent method - addOne(2) can be replaced by 3 without affecting the program's behaviour.

Note that referential transparency is a high-level concept, not a language feature (at least in Java).

user253751
  • 57,427
  • 7
  • 48
  • 90