# Deriving relation for gravitational self energy

My book says $U_{self}=\dfrac{-GM^2}{2R}$ for the hollow sphere, I tried deriving it as:

Suppose mass constructed is $m$, Work done on bringing mass $dm$ from $\infty$ to $R$ is $$dW=dm(V_{R}-V_{\infty})\\W=\int_{0}^{M}-\dfrac{Gm\cdot dm}{R}\\W=-\dfrac{GM^2}{2R}$$

By definition of Gravitation Self Energy,

The gravitational self-energy is equal to the amount of work done in assembling together its infinitesimal particles initially lying infinite distance apart.

$$\implies U_{self}=-\dfrac{GM^2}{2R}$$

But as $dV=\dfrac{dU}{m}$ by definition, also $dW=-dU$, so why when I used $dW=dm(dV)$, it works, I should have used $dW=dm(-dV)$.

In your first equation $W=-\dfrac{GM^2}{2R}$ on the left hand side $W$ is the work done by an external force and the right hand side is the change in gravitational potential energy of the system $\Delta U$ which in the case is the gravitational potential energy as the zero of potential energy has been taken to be the masses infinitely far apart.
Note that $W=+\Delta U$ and there is no negative sign in this equation if $W$ is the work done by the external force.