Understanding complex blocks

Report a typo

Here's a class containing one constructor and several initialization blocks:

class MagicNumber {

    private final static int[] NUMBERS;
    private static int next = 0;

    private int number;

    static {
        NUMBERS = new int[] { 1, 3, 7, 15, 31, 63 };
    }

    {
        this.number = NUMBERS[next % NUMBERS.length];
        next++;
    }

    public MagicNumber(int base) {
        this.number += base;
    }
}

Enter a sequence of numbers that is the result of the code below. Output only the number field of each instance. Separate the numbers by spaces in your answer.

MagicNumber[] numbers = new MagicNumber[8];

for (int i = 0; i < numbers.length; i++) {
    MagicNumber magicNumber = new MagicNumber(i);
}
Enter a short text
___

Create a free account to access the full topic