Exchange spring magnet
The magnetostatic energy is the energy stored in the field generated by a material's magnetic moments. The magnet's field reaches its maximum intensity if all the magnetic moments orient in one direction; this is what occurs in a hard magnet. In order to prevent building up the magnetic field, sometimes magnetic moments tend to form loops. That way, the energy stored in the magnetic field can be constrained; this is what occurs in a soft magnet. What determines whether a magnet is hard or soft is the dominant term of its magnetic energy. For hard magnets, the anisotropy constant is relatively large, making the magnetic moments align with easy axis. The opposite case applies to soft magnets, in which the magnetostatic energy is dominant.
Electromagnet
The material of a magnetic core (often made of iron or steel) is composed of small regions called magnetic domains that act like tiny magnets (see ferromagnetism). Before the current in the electromagnet is turned on, the domains in the iron core point in random directions, so their tiny magnetic fields cancel each other out, and the iron has no large-scale magnetic field. When a current is passed through the wire wrapped around the iron, its magnetic field penetrates the iron, and causes the domains to turn, aligning parallel to the magnetic field, so their tiny magnetic fields add to the wire's field, creating a large magnetic field that extends into the space around the magnet. The effect of the core is to concentrate the field, and the magnetic field passes through the core more easily than it would pass through air.