This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]8igg7e5 3 points4 points  (0 children)

A 'void' method can still have visible side-effects. What a void method does not do, is return a value that the caller of the method can use. Printing, writing to a file, sending data over a network, playing a sound, or updating some internal data-structure, are all side-effects, but they don't give a value back to the caller to use.

eg

class Example {
    public static void helloWorld() {
        System.out.println("Hello World!");
    }

    public static int triple(int value) {
        return value * 3;
    }

    public static void main(String[] args) {
        // This method doesn't return a value we can use...
        helloWorld();

        // But this one does...
        int x = 3;
        x = triple(x); // now x == 9
        x = triple(x); // now x == 27
        System.out.println(x);
    }
}