OK, so I copied the code from Johannes in hopes that I would be able to understand it and rewrite it myself, from memory. But no matter how I tried, I couldn't figure out the logic. Let me explain what puzzles me: In line 10 we are attributing out a new stream. But that stream isn't configured above.. What does it do? Then, in line 11 it calls the printSomething method. This in turn calls the system.out in lines 25-29. But how does it know what to do, when the logic for it is declared in lines 12 and 16-19 and even after out is reatributted the initial value in line 14. What kind of mystical sorcery time traveling law physics bending magic spell is this?
public class Solution {
    public static TestString testString = new TestString();

    public static void main(String[] args) {

        PrintStream consoleS = System.out;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream newStream = new PrintStream(baos);

        System.setOut(newStream);
        testString.printSomething();
        String[] str  = baos.toString().split("\n");

        System.setOut(consoleS);

        for (int i=0;i< str.length; i++) {
            System.out.println(str[i]);
            if (i%2 == 1)
                System.out.println("CodeGym - online Java courses");
        }
    }

    public static class TestString {
        public void printSomething() {
            System.out.println("first");
            System.out.println("second");
            System.out.println("third");
            System.out.println("fourth");
            System.out.println("fifth");
        }
    }
}