Answer
Before he became King of Bavaria, Maximilian I Joseph served as Prince-elector of Bavaria. This position was held by the head of the Wittelsbach family, which had ruled Bavaria since the 12th century. Maximilian I Joseph was elected prince-elector in 1799 and served in this position until 1806, when he was proclaimed King of Bavaria.