(ABC) Supreme Court Justice Anthony Kennedy is retiring after more than 30 years on the high court.

For more than three decades he’s reigned as the black-robed justice in the middle, the super-cerebral conservative from California who riled the right for casting ideology aside and forging an independent path that frequently led him to concur with his more liberal colleagues on the U.S. Supreme Court.

“It has been the greatest honor and privilege to serve our nation in the federal judiciary for 43 years, 30 of those years on the Supreme Court,” Kennedy said in a statement.

He said his decision to step down was based on his deep desire to spend more time with his loved ones.

In his formal letter to President Trump, Kennedy said his resignation would be effective July 31.

“For a member of the legal profession it is the highest of honors to serve on this Court,” he wrote. “Please permit me by this letter to express my profound gratitude for having had the privilege to seek in each case how to best know, interpret, and defend the Constitution and the laws that must always conform to its mandates and promises.”

Kennedy’s tenure spanned six presidential administrations, the longest term of any of the current sitting justices.

He was appointed by President Ronald Reagan in 1988 to fill the seat of Justice Lewis Powell Jr., who announced his retirement in June 1987.

While he had long been on the Republican icon’s short list of Supreme Court candidates, Kennedy was not Reagan’s first choice or even his second.

Reagan only nominated Kennedy when his top pick, Robert Bork, the ultra-conservative federal judge who Sen. Ted Kennedy warned would take the country back to a time of “back-alley abortions” and “segregated lunch counters,” was rejected during the Senate confirmation process.

Reagan’s second pick, Douglas Ginsburg, flamed out after he admitted that “on a few occasions” he used marijuana while he was an assistant professor at Harvard University in the 1970s.

Kennedy learned he was officially Reagan’s third choice just 15 minutes before it was announced in the Briefing Room of the White House on a snowy November day in 1988.

“He’s a hard worker and, like Justice Powell, whom he will replace, he is known as a gentleman,” Reagan said of Kennedy at the time. “Judge Kennedy is what many … have referred to as a true conservative — one who believes that our constitutional system is one of the enumerated powers — that it is we, the people who have granted certain rights to the government, not the other way around.”

The U.S. Senate confirmed Kennedy on Feb. 4, 1988, with a unanimous vote of 97-0.

Sen. Strom Thurmond of South Carolina, the ranking Republican on the Judiciary Committee, praised Kennedy as “an advocate of judicial restraint.” While Sen. Kennedy, no relation, lauded him as someone who “believes that the Constitution is not a fossil frozen in the past, but a living document … capable of responding to contemporary threats to fundamental rights and liberties.”

At the age of 51, Kennedy was sworn in as the 104th justice of the U.S. Supreme Court by then-Chief Justice William Rehnquist on Feb. 18, 1988.

“In this the year of the [United States] Bicentennial, it is appropriate to recognize an essential truth, and that is that the Constitution of the United States is the single fact, the single reality, the single moral principle that sets the United States apart from other nations now and throughout history. I shall honor the Constitution,” Kennedy said in his swearing-in speech at the White House.

He took a seat reserved for rookie justices on the far left side of the court but was initially considered a feather in the conservative wing headed by Rehnquist and two other Reagan picks, Antonin Scalia and Sandra Day O’Connor, the first woman to serve on the high court.

Early in his Supreme Court tenure, Kennedy voted with Rehnquist and the conservative bloc 90 percent of the time, including striking down an affirmative action program requiring set-asides for black-owned construction firms and upholding a law requiring minors seeking abortions to first get permission from a parent.

But Kennedy also showed early on that his decisions weren’t based on political ideology, siding with his liberal colleagues in 1989 to uphold a ruling that protesters had a Constitutional right to burn the American flag.

In 1992, he voted with the liberal wing again, ruling that legal restrictions on access to abortion must not constitute an “undue burden” on a woman’s right to abortion established in the 1973 Roe vs. Wade decision.

He also voted to extend constitutional protections to gays and lesbians, writing the 1996 opinion striking down a Colorado constitutional amendment denying homosexuals the right to file local discrimination claims. He also sided with the court’s liberal wing in 2003 in declaring unconstitutional a Texas law criminalizing sodomy between consenting adults of the same sex.

But many conservatives believed that Kennedy redeemed himself when he cast the deciding vote in the landmark case of Gore vs. Bush that ended a recount in Florida of the presidential election and sent Republican George W. Bush to the White House.

In 2015, Kennedy was again slammed as a conservative traitor when he cast the deciding vote in two landmark cases.

Not only did he cast the deciding vote guaranteeing the right to same-sex marriage, he wrote the majority opinion: “It would misunderstand these men and women to say they disrespect the idea of marriage. Their plea is that they do respect it, respect it so deeply that they seek to find its fulfillment for themselves. Their hope is not to be condemned to live in loneliness, excluded from one of civilization’s oldest institutions. They ask for equal dignity in the eyes of the law. The Constitution grants them that right.”

That same year, Kennedy voted to uphold a key component of the 2010 Affordable Care Act, also known as Obamacare, allowing the federal government to provide nationwide tax subsidies to help Americans buy health insurance.

While Kennedy became known for his so-called “swing votes,” he hated the term.

“It has this visual image of the spatial gyrations. The cases swing, I don’t,” he said in a 2015 appearance at Harvard Law School.

Born and raised in Sacramento, California, Kennedy served as an altar boy at Holy Spirit Catholic Church. He grew up in the same neighborhood as famed writer Joan Didion and actor Max Baer Jr., who went on to play Jethro Bodine in the “Beverly Hillbillies” TV series.

His father, Anthony “Bud” Kennedy Sr. was a Republican lobbyist for tobacco interests and founded his own law firm, while his mother, Gladys, was a teacher who had graduated from Stanford University in 1928.

At the age of 9, Kennedy, with the help of his father, got a job as a page in the California State Senate, where he was introduced to Gov. Earl Warren, who later would become the 14th chief justice of the U.S. Supreme Court. In a letter to a young Kennedy, which he donated to the Supreme Court Historical Society, Warren wrote, “You’re going to go very far in government.”

Kennedy graduated as an honor student from McClatchy High School in Sacramento and, like his mother, attended Stanford University, where he earned a bachelor’s degree in political science in 1958 after spending his senior year at the London School of Economics.

He went on to earn a law degree at Harvard Law School, graduating in 1961. Kennedy returned to California and started practicing law in San Francisco.

In 1963, he married Mary Davis, a Sacramento schoolteacher. That same year, his father suffered a fatal heart attack, and Kennedy moved back to Sacramento to take over his father’s law firm.

While teaching Constitutional law at University of the Pacific’s McGeorge School of Law in Sacramento, Kennedy was recruited in 1973 to draft a tax-cutting referendum for then-Gov. Ronald Reagan, which was defeated at the polls.

Reagan appreciated Kennedy’s help on the referendum and recommended him to President Gerald Ford. When a position came open at the U.S. Court of Appeals for the Ninth Circuit in 1975, Ford appointed Kennedy, who was only 38 years old at the time, to the bench.

During his nearly 13 years on the appeals court, Kennedy wrote nearly 500 opinions, forging a reputation as a conservative jurist.

Once asked what makes a good judge, Kennedy answered, “You will never be a good judge unless you ask yourself that question every day. A part of judging is to ask, ‘Why am I doing this?'”

Kennedy said his passion was always being a lawyer.

“I still miss being a lawyer after some years on the bench. I would be happy practicing law. I loved it,” he said in a 2009 C-SPAN interview. “You have an oath, an obligation to defend something that is really basic to the American identity, to the American idea to freedom as we know it. The idea that the government cannot arrest your client without cause, convict your client without proof beyond reasonable doubt is very impressive.”