Why does Hamlet think that Denmark is a prison?William Shakespeare's "Hamlet"
It's an expression of Hamlet's frustration at the growing restrictions placed upon him by Claudius. It also says something about Hamlet's view of Denmark under his uncle's growing tyranny. Claudius has morally corrupted the country, having secured the throne through an act of murder. There really is "something rotten in the state of Denmark" with Claudius in charge.
At the same time, Hamlet gives the impression of being the only one who seems to feel imprisoned by Claudius's rule:
"Why, then, ’tis none to you, for there is nothing either good or bad, but thinking makes it so. To me it is a prison." (act 2 scene 2)
It's not surprising that Rosencrantz, for example, doesn't feel the same way about Denmark as Hamlet. After all, pursuing the prison metaphor further, he's one of the jailers. Or, at the very least, he's a spy sent by Claudius to keep a close eye on Hamlet. In any case, Hamlet seems to be the only one resisting his uncle's morally corrupt tyranny. That, more than anything else, makes him feel like he's in prison.
Remember, when Hamlet refers to Denmark as a prison, he is speaking to Rosenkrantz and Guildernstern:
What have you, my good
friends, deserved at the hands of Fortune that she sends you
to prison hither?
He knows that the pair have been sent for and that they are spying on him. He also knows that whatever he says will go back to his murderous uncle, King Claudius. Now also remember why he is still living in the castle and not back at school: Claudius, the King, has requested that he remain there, under his watchful eye. He is a prisoner of the King and he wants the King to know he feels that way.
And further, as noted above, he is a prisoner of unpleasant and pressing circumstances that have, much to his displeasure, fallen upon him and constricted his accustomed freedom of motion and action. He's caught in Denmark by decree and circumstance. A prison, indeed.
In Act 2, Scene ii Hamlet refers to Denmark as a prison because he feels so trapped in his life there and feels helpless to change his situation, as if he were locked into it like a prison cell.
In the case of Hamlet, any of the control he thought he had, fell away with the murder of his father. Having his father, the king, be killed by his own brother has sent Hamlet slightly mad. This feeling of helplessness and feeling out of control is exacerbated by the fact that he is cooped up in a palace with no real outlet.