To examine the association between socioeconomic position (SEP) over the lifecourse and cognitive function in middle age. Two alternative models were assessed: the "direct effects model" where temporally distinct measures of SEP have only direct effects on adult cognition, and the "indirect effects" model where the effect of early life SEP are mediated through later life measures of SEP. Data are from the Whitehall II study (N=10,308 at baseline), a longitudinal cohort study of British civil servants, aged between 46 and 68 years at the time of cognitive testing. Structural equation models were used to compare the fit of direct and indirect effects models, and quantify the effects of different measures of SEP on cognition. Childhood SEP, education, and adult SEP were used to model SEP across the lifecourse. Cognitive function was assessed as a latent construct composed of the following: verbal memory, AH 4-I, Mill Hill, phonemic and semantic fluency. The indirect effects model provided a better fit to the data. Childhood SEP had no direct effect on cognitive function but had a substantial "indirect effect," mediated through education and adult SEP. 78.4% of the effect of education in men and 100% in women was indirect. Socioeconomic differences in adult cognition are a result of the socioeconomic trajectory of individuals throughout their lifecourse. Early measures of SEP influence cognition indirectly, through their influence on later measures of SEP.