Hamlet's thoughts about suicide are all contained in his famous soliloquy in Act 3, Scene 1, beginning with the words "To be, or not to be: that is the question." He does not seem to have any moral or religious objections to suicide. He tells himself that "Tis a consummation / Devoutly to be wished." (It is significant that he uses the word "devoutly," since this word has religious connotations.) Not only does he seem to be contemplating suicide, but, characteristically, he thinks about the matter in general terms as it applies to humanity as a whole. He speculates quite reasonably that a great many people would kill themselves "but that the dread of something after death" frightens them from doing what is in everyone's power to do. This soliloquy is famous because so many people have had similar thoughts and similar fears--at least at certain times. Death seems like an easy way to escape life's problems, except that nobody can know beforehand what death is actually like. It is an "undiscovered country from which no traveler returns" to describe it to the living. Hamlet's own father tells him in Act 1, Scene 5, that he is forbidden "To tell the secrets of my prison-house" but suggests that death is so horrible that a live person like Hamlet could not bear to hear the truth about it. Hamlet seems to have no other objection to suicide.
Hamlet's thoughts on suicide occur at two places in the play. The first is in his first soliloquy in Act 1 Scene 2 where he recognizes that "self-slaughter" is against God's commandment (i.e. Thou shall not kill). Here, Hamlet is being over dramatic: youthful hyperbole. His morose expressions establish his youth and an unrealistic view of death. His mood is sullen having suffered the tongue lashing from Claudius and Gertrude. Denied his studies, the crown, the alliance of his mother and the guidance of his father, Hamlet feels like he has no purpose. He says it is only the stain of sin that keeps him from killing himself. This all changes when his father's spirit challenges him.
Second at the end of the play as Hamlet lays dying, Horatio expresses the desire to follow Hamlet in death. Hamlet rejects Horatio's attempt. Like the Ghost's injunction to Hamlet, Horatio has a purpose. He wants Horatio to carry on Hamlet's memory and champion his good name. Remember me.
Much ink is spilled over the idea that Hamlet's soliloquy in 3.1 expresses suicidal ideation. This interpretation is misguided. Toward the end of his speech Hamlet does mention that life can be surrendered by a mere bodkin. Here, Hamlet is expressing the frailty of the human body and the ease with which life can be taken. The difficulty in taking a life does not lie in the body's resistance to harm; rather, it is the resistance of the will. This furthers the thought on the hesitation between resolution and action which is the theme of the soliloquy as a whole. Hamlet is not worrying about
killing himself he is concerned about being killed at the hands of another. More generally (and Hamlet is frequently occupied in searching for universal principles) Hamlet realizes that taking arms against a sea of troubles, i.e., meeting the day-to-day travails of life's burdens, ultimately shortens ones life.
This topic comes up as it does between Hamlet's plan to catch the conscience of the king and "The Mousetrap" because Hamlet realizes that crossing the boundary from plan to action puts him in direct opposition to the king's power. The ultimate aim is to take the king's life, Hamlet recognizes that in doing so he jeopardizes his own. Though this may prompt a notion that the plan is "suicidal", it really isn't about a desire to die as much as it is a comment on the dangers involved.