A US court has sentenced Brandon McInerney, the Californian student who last month pleaded guilty to the second-degree murder of gay classmate Larry King, to 21 years in prison.

McInerney’s prison sentence is part of a plea bargain he made with the court in Ventura County, California.

McInerney was 14 when he shot King in the back of the head during a computer science lesson in 2008.

Because of the guilty pleas, he will not be eligible for early release on grounds of good behaviour.

Initially, McInerney had claimed that King, who identified as gay and was allowed to wear feminine clothes by the staff at Green Middle School, Oxnard, had teased and provoked him.

McInerney claimed he had acted without thinking when he shot his classmate, but the court was told he had informed a fellow student of his intention to kill King the night before the shooting.

A mistrial was declared in September when jurors were unable to decide whether McInerney had committed first- or second-degree murder, or manslaughter.

Shortly after the mistrial, McInerney admitted second-degree murder, voluntary manslaughter and use of a firearm.

In 2009, McInerney’s father was found dead, from an accidental head injury caused by a fall, on the day he was due to appear in court.

The plea deal means McInerney will serve 11 years for voluntary manslaughter and 10 years for the use of a firearm, but in exchange for pleading guilty, he will not serve time for the murder charge.

At the time, McInerney’s attorney Robyn Bramson told Reuters: “It’s very unorthodox. Both sides wanted to resolve it for various reasons, and this was just our way of getting to something that was tolerable for both sides.”

McInerney, who has already served nearly four years in custody, was given the maximum sentences for the manslaughter and firearm charges.

McInerney’s defence lawyer said: “He feels deeply remorseful and stated repeatedly if he could go back and take back what he did he would do it in a heartbeat.”

But Greg King, the victim’s father, addressed McInerney during the sentencing, saying: “You have left a big hole in my heart where Larry was and it can never be filled.”