In discussions of ethics, lawyers and judges like to talk about the need to avoid "even the appearance of impropriety."

As public servants whose legitimacy depends on public confidence that they can rule impartially, judges face ethical constraints that limit their activities not only on the bench, but also off it.

The tradition of bending over backward to avoid any implication of bias was what made so startling the disclosure Thursday by D.C. Superior Court Judge Joseph Michael Hannon -- during a trial of abortion protesters -- that he and his law clerk had marched in the same demonstration as the defendants before him.

"I, too, exercised my First Amendment rights against" the Supreme Court's 1973 Roe v. Wade decision legalizing abortion, Hannon explained in court Thursday.

By Friday, Hannon had rethought his position and announced that he had decided to remove himself from hearing the nonjury trial. Hannon's actions highlighted the problem of judges making public their views on issues apt to come before them.

Under a 1973 opinion by the Federal Advisory Committee on Judicial Activities, federal judges are admonished not to be members of oranizations whose court cases could come before them, such as the NAACP, the Anti-Defamation League of B'nai B'rith and the Sierra Club.

Even a courtroom attendant may not serve as an officer of the local NAACP branch, the New Jersey Supreme Court ruled last month. That restriction is necessary, the court said, in order to assure "every person walking into that court . . . that the judge and the entire court are dispassionate and impartial."

As Hannon himself pointed out Friday, if he had acquitted the abortion protesters, he would have been accused of acting out of sympathy with their views. If he had found them guilty, he would have been subject to complaints from the defendants that he had overcompensated for his opposition to abortion -- and the public's awareness of it.

With one complaint about Hannon's actions already filed with the D.C. Commission on Judicial Disabilities and Tenure, the propriety of Hannon's participation in the march -- along with his failure to reveal it earlier -- remains an issue.

"The First Amendment has its application to the judiciary, but a judge's right to freedom of expression and association must be balanced against the public's right to an impartial judiciary," Northwestern University law Prof. Steven Lubet wrote in a 1984 book on judicial ethics. In Hannon's case, Lubet said in an interview Friday, merely participating in the march was appropriate as long as Hannon did not call attention to the fact that he is a judge.

But Yale Law School Prof. Geoffrey C. Hazard Jr., who served as a consultant to the American Bar Association committee that wrote the judicial ethics code, said that while Hannon "has First Amendment rights, they are not the same as other people's . . . . There are lots of things I can do as a law professor that judges can't do as judges. If he doesn't like that, he ought to get off the bench."

While those familiar with District judges said they could not remember another instance of a judge participating in a demonstration, courts elsewhere have grappled with similar issues.

The Massachusetts Supreme Judicial Court in 1978 suspended the chief judge of the state's superior court for attending a benefit to raise money for defendants in a case that was before the court. Although the judge said he had attended the affair for educational purposes, the court found that he "compromised his position by seeming to . . . have particular sympathy with the views of the partisan group which sponsored the affair."

However, the Florida Supreme Court in 1982 dismissed charges of improper conduct against a trial judge who wrote newspaper articles and letters expressing his opposition to capital punishment, which had been adopted in the state.

Although what the judge did "came close to the dividing line," the court said, "There is no doubt that a judge in an appropriate forum may express his protest, dissent and criticism of the present state of the law, as long as he does not appear to substitute his concept of what the law ought to be for what the law actually is."

One of the most problematic of Hannon's actions was that he failed -- through "inadvertence," he said in court Friday -- to reveal his participation in the march until after the trial had started, and the constitutional protection against double jeopardy was triggered.

Although the U.S. attorney's office said it believed the defendants could be retried, criminal law experts said the issue was far from clear. If Hannon had been forced to withdraw because he became ill in the middle of the trial, it is certain that the defendants could be retried without violating the double jeopardy clause. This situation is trickier because Hannon had it within his control to make the disclosure and, if necessary, step aside, before jeopardy had attached.

In Illinois v. Sommerville, a leading Supreme Court case on double jeopardy, the court in 1973 found that it did not constitute double jeopardy to subject a defendant to a second trial when a judge halted the first trial midway because the indictment was technically flawed -- a matter within prosecutors' control.

Still, "this is not an obvious case," Michigan Law School Prof. Jerry Israel, author of a leading criminal law treatise, said of the Hannon situation. "The law is hardly clear on that."