When sodium reacts with excess of oxygen, oxidation number of oxygen changes from:
0 to -1
2Na + O2(excess) → Na2O2
The oxidation state of oxygen is 'zero' in its native state, where as its oxidation state in its peroxide form is (Na2O2) is -1.
You have to go other way round to calculate the oxidation state of oxygen in the peroxide. 2 × (+1) + x = 0 gives us x = −2. Therefore each oxygen has an oxidation state of −22 = −1.