Heinlein moved substantially politically over the years, from being a firm Democrat in his younger years (campaigning for the Democrats and attempting to win a seat to the California State Assembly as a Democrat in 1938, to becoming conservative in the early 50's, and right-libertarian and eventually describing himself as anarchist or autarchist (economically right wing anarchism, basically) by the 60's, and a lot of his works reflects pretty directly his political position at the
time of writing.
Starship Troopers is sometimes viewed as his "parting shot" to the left wing, though it was written several years after he ceased to consider himself a Democrat.
While it probably wasn't as militaristic as it appears at a casual glance (he's elaborated on the ideas elsewhere in ways that further "softens" it), it did come at a time where his political views were evolving towards a more conservative and more authoritarian ideal (though that soon changed again).