WASHINGTON — Supreme Court Justice Anthony M. Kennedy announced Wednesday that he will retire at the end of next month, preparing the way for the most significant change in the court's makeup in half a century.

The vacancy will allow President Donald Trump to make the U.S. Supreme court a solidly conservative body for years, if not generations, to come — a towering legacy of his time in office.

Kennedy's departure on July 31, which had been rumored for the last year, could also put in doubt the future of a nationwide right of access to abortion.

"Justice Kennedy was the most important member of the court in a century, maybe ever," said Tom Goldstein, a Washington lawyer who argues frequently before the court and publishes the SCOTUSblog web site.

Kennedy turns 82 in July and is the court's second-oldest justice. Ruth Bader Ginsburg is 85.

Since 2006, when moderate conservative Sandra Day O'Connor left the court, Kennedy has been the swing justice, often casting the deciding vote in the most high-profile cases.

Joining the court's four other conservatives, he voted to gut the landmark Voting Rights Act, allow corporations and unions to spend unlimited funds to support candidates and give new life to the Second Amendment right to own a gun.

But he joined the liberals in banning capital punishment for the youngest offenders, declaring that prisoners at Guantanamo Bay had a right to challenge their detentions and limiting the powers of the states to enforce their own tough immigration laws.

Supreme Court Justice Anthony Kennedy gives the thumbs-up after taking the constitutional oath at the White House on Feb. 18, 1988 as President Ronald Reagan looks on. Doug Mills

He profoundly shaped the court's rulings on gay rights, writing four of its most important decisions, including the landmark 2015 ruling that struck down bans nationwide on same-sex marriage.

"It's not just that he was the pivotal vote so often. It's that his thinking changed the country," Goldstein said. Gay rights is the biggest example, but it's not the only one."

Any nominee chosen by Trump is sure to be more conservative than Kennedy. The president would likely pick someone as ideologically to the right as Neil Gorsuch, who has voted with the court's other conservatives in nearly every case since taking his place on the bench just over a year ago.

President Ronald Reagan nominated Kennedy, a fellow Californian, in 1987 after the Senate rejected Robert Bork as too rigid and after a second nominee, Douglas Ginsburg, admitted smoking marijuana.

At first a reliable conservative, Kennedy soon broke away and voted to uphold the court's 1973 Roe v. Wade abortion decision. But 14 years later, he wrote the ruling that approved a federal ban on so-called partial-birth abortions.

A Trump-nominated successor to Kennedy would likely become the court's fifth reliable conservative, joining Chief Justice John Roberts and Justices Clarence Thomas, Samuel Alito and Gorsuch. Because only Thomas has declared opposition to Roe v Wade, it's uncertain whether opponents of abortion would have the five votes needed to overturn it.

At age 69, Kennedy had a stent placed in one of his heart arteries, and underwent the same procedure a year later. But neither kept him off the golf course, and his health has appeared sound.

A year ago, he told friends he was considering stepping down, but he returned to the bench last October for the start of the current term. His departure will end nearly 31 years of service on the Supreme Court.