WASHINGTON — Overturning a 1985 precedent, the Supreme Court on Friday ruled that plaintiffs may sue in federal court to seek compensation as soon as state and local governments take their property through eminent domain. The earlier ruling had required plaintiffs to sue in state court.

The vote was 5 to 4, with the court’s more conservative justices in the majority. The decision was notable for an exchange about the court’s attitude toward precedent.

Writing for the majority, Chief Justice John G. Roberts Jr. said overruling the 1985 decision was justified because it “was not just wrong.

“Its reasoning was exceptionally ill founded,” he wrote. He added that the decision had “come in for repeated criticism over the years from justices of this court and many respected commentators.”

That analysis was worrisome, Justice Elena Kagan wrote in dissent, adding that “today’s opinion smashes a hundred-plus years of legal rulings to smithereens.”

“He wrote of the dangers of reversing legal course ‘only because five members of a later court’ decide that an earlier ruling was incorrect,” Justice Kagan wrote. “He concluded: ‘Today’s decision can only cause one to wonder which cases the court will overrule next.’”

Friday’s case was brought by Rose Marie Knick, who owns 90 acres of land in rural Pennsylvania near Scranton. Her property includes a small family cemetery.

In 2012, the local township passed an ordinance requiring all cemeteries to be open to the public during daylight hours. Ms. Knick sued in federal court, saying the ordinance violated the Fifth Amendment’s takings clause, which says private property may not be taken for public use without just compensation.

Chief Justice Roberts said the 1985 decision was wrong. “Contrary to Williamson County,” he wrote, “a property owner has a claim for a violation of the takings clause as soon as a government takes his property for public use without paying for it.”

“A later payment of compensation may remedy the constitutional violation that occurred at the time of the taking, but that does not mean the violation never took place,” the chief justice wrote. “The violation is the only reason compensation was owed in the first place. A bank robber might give the loot back, but he still robbed the bank.”

In dissent, Justice Kagan wrote that Chief Justice Roberts misunderstood the takings clause, which she said was not violated until the government failed to pay following proceedings about how much was owed.

“Today’s decision thus overthrows the court’s long-settled view of the takings clause,” she wrote. “The majority declares, as against a mountain of precedent, that a government taking private property for public purposes must pay compensation at that moment or in advance.”

The decision in the case, Knick v. Township of Scott, No. 17-647, she wrote, “will subvert important principles of judicial federalism.”

“Today’s decision sends a flood of complex state-law issues to federal courts,” Justice Kagan wrote. “It makes federal courts a principal player in local and state land-use disputes. It betrays judicial federalism.”

But Justice Kagan reserved her harshest criticism for the majority’s attitude toward to power of precedent. “It is hard to overstate the value, in a country like ours, of stability in the law,” she wrote.