WASHINGTON -- The Supreme Court took two cracks at one of the law's thorniest questions Monday: When can you lock up a prisoner and throw away the key?

Not when it's a teenager who hasn't killed anyone, the justices said. But when it's a "sexually dangerous" inmate, maybe so, even if he has completed his federal prison sentence.

By a 5-4 vote, the court said young people serving life prison terms must have "a meaningful opportunity to obtain release" if they haven't killed their victims. The majority opinion by Justice Anthony Kennedy extended the "children are different" rationale that drove his decision five years ago that outlawed the death penalty for killers under 18.

The court ruled in the case of Terrance Graham, who was implicated in armed robberies when he was 16 and 17. Graham, now 23, is in prison in Florida, which holds 60 percent of juvenile defendants who are locked up for life for crimes other than homicide.

"The state has denied him any chance to later demonstrate that he is fit to rejoin society based solely on a non-homicide crime that he committed while he was a child in the eyes of the law," Kennedy wrote in his majority opinion. "This the Eighth Amendment does not permit."

In a second case, the court voted 7-2 to uphold a federal law that allows for the indefinite imprisonment of inmates considered mentally ill and "sexually dangerous," no matter that their sentences have been served.

Solicitor General Elena Kagan successfully argued the government's case in front of the Supreme Court in January. Kagan has now been nominated to replace the retiring Justice John Paul Stevens.

Kagan compared the government's power to commit sexual predators to its power to quarantine a federal inmate whose sentence has expired but who has a highly contagious and deadly disease.

The decision is in keeping with previous high court cases that have upheld state civil commitment laws for sexual predators. States hold the vast majority of sex offenders who are in prison.

In both cases, the court's liberal justices held sway and Justices Antonin Scalia and Clarence Thomas were in dissent.

Life sentences with no chance of parole are rare for juveniles tried as adults and convicted of crimes less serious than killing, although roughly three dozen states allow for the possibility of such prison terms. Kennedy said 129 inmates in the United States are serving such terms.

Those inmates are in Florida, the federal system and 10 other states -- California, Delaware, Iowa, Louisiana, Mississippi, Nebraska, Nevada, Oklahoma, South Carolina and Virginia -- according to a Florida State University study that the court supplemented with independent research. More than 2,000 other juveniles are serving life without parole for killing someone. Their sentences are not affected by Monday's decision.

In Charleston, 9th Circuit Public Defender Ashley Pennington said he doesn't think the ruling will radically affect prosecutions in South Carolina, largely because the mandate from the high court is that states be required to at least offer the possibility of a parole opportunity. He does, however, expect the S.C. Legislature will probably adjust state law accordingly.

First Circuit Public Defender Mark Leiendecker of Dorchester County also agreed the mandate will not drastically change cases in the state, saying most defendants here under 18 already are getting special considerations as their cases move forward.

Ninth Circuit Solicitor Scarlett Wilson, whose office handles prosecutions in Charleston and Berkeley counties, said she could not recall a case locally involving a life-without-parole sentence given to a juvenile in a non-murder case.

Wilson did have some disagreement with the new ruling, saying it will take some of the decision-making out of the hands of judges and prosecutors. Anther weakness she cited is that it could give defendants access to parole in cases in which a victim of a violent crime is strong enough to survive an attack that otherwise might have become a homicide.

Schuyler Kropf of The Post and Courier contributed to this report.