The magnetic field \( B \) within a solenoid is expressed as \( B = \mu_0 n I \), where \( n \) denotes the turns per unit length, \( I \) represents the current, and \( \mu_0 \) is the magnetic permeability of a vacuum. The energy density \( u \) of a magnetic field is defined as \( u = \frac{U}{V} \), with \( U \) being the total stored magnetic energy and \( V \) the volume. The magnetic energy \( U \) is quantified by \( U = \frac{1}{2\mu_0} \int B^2 \, dV \). For a uniform magnetic field inside the solenoid, the energy density simplifies to \( u = \frac{B^2}{2\mu_0} \). Therefore, the magnetic energy density is \( \frac{B^2}{2\mu_0} \).