Self-sacrifice is required in Jewish law for rare yet specifically defined circumstances, in which a Jew is expected to sacrifice their own life rather than violate a religious prohibition.
The core principle of self-sacrifice, yehareg ve'al ya'avor ("let him be killed rather than transgress"), is enunciated in a Talmudic sugya (pericope) at Sanhedrin 74a-b and thereafter typically discussed in terms of three cardinal or exceptional prohibitions.
Many more ritual prohibitions exist as well, which means that under limited circumstances a Jew has to self-sacrifice when the greater good calls for breaking a more minor dictate.
However, three areas of prohibition may not be trespassed under any circumstances, even to save a human life: acts involving murder, some kinds of sexual misconduct, and idol worship.
In tractate Sanhedrin 74a, the Talmud records: "Rav Yochanan said in the name of Rav Shimon ben Yehotzadak: 'It was decided by a vote in the loft of the house of Nitezeh in Lod: For all the sins in the Torah, if a person is told, "Transgress and you will not be killed," they should transgress and not be killed, except for idol worship, sexual relations and bloodshed.'"
A famous example can be found in the Babylonian Talmud Gittin 57b, the apocryphal II Maccabees 7, and other sources about Hannah and her seven sons, a story associated with the holiday of Hanukkah.
This story, however, relates another exception where Halakha requires that one surrender one's life: a situation in which a person is forced to break a law for the sake of desecrating the Torah.
Hannah and her sons acted in this way when it came to eating pork for the sake of desecrating the Torah; by allowing themselves to be killed, they sanctified God's name in public.
The exception of murder is derived by logic, as the Talmud states (on Pesachim 25b, Yoma 82b, and Sanhedrin 74a): Someone came before Rava[a] and said, "The governor of the city ordered me to slay a certain man, and threatened to kill me if I did not".
The desecration of God's name is considered the harshest violation of Jewish law, at least as far as heavenly forgiveness is concerned; therefore, if the sin is to be committed in public (for these purposes, in the presence of ten Jewish male adults), and the sole purpose of the persecutor is to have the Jew transgress halakha, any prohibition would be considered a matter of yehareg v'al ya'avor.
For example, if a Jew is being forced to transgress the Shabbat laws for the sake of the forcer's personal profit, he or she would not be required to give his or her life.
According to Maimonides, in a situation where one is not required to sacrifice oneself rather than transgress, to do so would be considered suicide, which is strongly forbidden and condemned under Jewish law.
Following through and sacrificing one's life in accordance with the law of yehareg ve'al ya'avor is considered to be Kiddush Hashem (sanctification of God's name).