The Kingdom of Norway as a unified realm was initiated by King Harald I Fairhair in the 9th century. His efforts in unifying the petty kingdoms of Norway resulted in the first known Norwegian central government. The country however fragmented soon, and was collected into one entity in the first half of the 11th century. Norway has been a monarchy since then, passing through several eras.
Thus was born the medieval (or, as is sometimes said, the first independent) kingdom of Norway, the realm of the Fairhair dynasty.
According to the traditional view, Norway was the hereditary kingdom of the 'Fairhair' dynasty, agnatic (patrilineal) descendants of the first unifier-king, Harald Fairhair. The successors to the throne after year 872 were all placed among Harald's male descendants. In the 13th century, the kingdom was officially declared hereditary by law, contrary to the other Scandinavian monarchies which were elective kingdoms in the Middle Ages.
Harald Fairhair was the first king of all Norway, as opposed to being one of several contemporaneous kings in Norway. The traditional date of the first formation of a unified Norwegian kingdom is set to 872 when he defeated the last petty kings who resisted him at the Battle of Hafrsfjord, though the consolidation of his power took many years. The boundaries of Fairhair's kingdom were not identical to those of present-day Norway and upon his death the kingship was shared among his sons. Harald Fairhair unified Norway, at least the coastal areas north to Trøndelag. After his death, the fragmentation back into petty kingdoms happened almost instantly. However, most of them were now in the hands of Harald's putative sons, descendants or allies. Although there were districts in the hands of other dynasties (such as Ladejarls), the concept of a central power on an hereditary basis had come into existence. It remains uncertain whether Norway can be defined as an hereditary kingdom even after the successions of Eric I of Norway and Haakon I of Norway, sons of Fairhair himself. Some historians put emphasis on the actual monarchical control over the country and assert that St. Olav, who reigned from 1015, was the first king to have control over the entire country. Olav is traditionally held to be the driving force behind Norway's final conversion to Christianity. He was later also revered as Rex Perpetuum Norvegiæ (Latin: eternal king of Norway). Only when the "half-brothers" Olav II and Harald III ascend to power, is there any weight given to the claim that the successor was predestined by some rules of inheritance and not simply through force.