Side effect–what’s this?

A side effect is anything a method does besides computing and returning a value. Any change of instance or class field values is a side effect, as is drawing something on the screen, writing to a file or a network connection.

Strictly speaking, a “function” is defined as not having side effects – which is why Java uses the word “method” instead. A real function with no return value would be pointless.

Obviously, a method that does not have a return value must have some sort of side effect that justifies its existence. Set methods are an example – the side effect is changing the object’s internal state.

Leave a Comment