There are a few exceptions to this rule, such as in peroxides and superoxides, where oxygen has an oxidation state of -1 and -1/2, respectively.
In O2, each oxygen atom has an oxidation state of 0. This is because the two oxygen atoms are bonded to each other and therefore do not have any net charge.