We examine the effect of single-sex classes on the educational attainment of students within a coeducational university. Before students arrived on campus, we randomly assigned them to all-female, all-male, and coed classes, and thereby avoid the selection issues present in earlier studies on single-sex education of students in primary and secondary school. We find that 1 h a week of single-sex classes benefits women: females score a quarter of standard deviation better overall and are 7.7% more likely to pass their first year course. Furthermore, women assigned to all-females classes in their first year are roughly 57% less likely to drop out of university and are 61% more likely to get a top ranked degree under the UK system. There is evidence that single-sex classes cause women to adopt behaviors associated with better academic outcomes, such as attending more classes and doing optional assignments. However, these behavioral changes cannot explain much of the all-female effect.