Contrary to popular belief, this is not a cocoon. Only certain moths build cocoons, which are like a silky sleeping bag that covers the insect. This, on the other hand, is what's called a chrysalis.
The caterpillar, along with most others in the Saturniidae family, spins a silk cocoon. This is where it then spends its time as a pupa, the stage of development before the moth becomes an adult. The ...