This is a consequence of the virial theorem of mechanics.
https://en.wikipedia.org/wiki/Virial_theorem
This theorem also lets you conclude that as a nondegenerate star becomes more tightly bound (smaller, for a given mass) it must also become hotter.
(Why did someone downvote this?)