WASHINGTON (AP) _ When he quit the federal bench in 1987 after 13 years of service, Herbert J. Stern harked back to his days as New Jersey's premier prosecutor and said: ''I think in time, if I'm lucky, people will forget I was a judge. I want them to say, 'There's a lawyer.'''

The Iran-Contra affair will give him an opportunity to burnish that image.

Stern was retained this week as an outside counsel to Iran-Contra special prosecutor Lawrence E. Walsh, with special responsibility for fending off potential defendants' assertions that immunity grants from Congress bar prosecution.

The job, which Stern will handle while continuing to represent other clients of his New Jersey law practice, will make the former judge and prosecutor a major player in any pre-trial maneuvering likely to take place if indictments are returned.

Among those under investigation by Walsh's office are former White House aides John Poindexter and Lt. Col. Oliver North, who testified before Congress under grants of immunity.

Stern, 51, became a leading expert on immunity in the early 1970s, when immunized former members of criminal conspiracies were the leading government witnesses at political corruption trials he prosecuted in New Jersey.

His most celebrated case involving witnesses with immunity led to the conviction in 1970 of Newark Mayor Hugh J. Addonizio and broke up a multimillion-dollar extortion ring in that city's government.

Over the next three years, first as deputy and then as chief U.S. attorney for New Jersey, Stern prosecuted some 150 politicians. Many of them were convicted, including Democratic congressman Cornelius Gallagher, Jersey City Mayor Thomas Whelan, city commissioners in Atlantic City, aides to several governors and the state chairmen of both political parties.

Opposing lawyers credited his success to careful preparation, a polite intensity that riveted jurors' attention and an uncanny cross-examination skill that left opposing witnesses squirming visibly.

When he was appointed to the U.S. District Court in 1973, acquaintances questioned how happy he would be judging the action, rather than taking part.

If that was a problem, it didn't remain so.

In 1979, he got into a blistering battle with the State Department that prompted him to write a book, ''Judgment in Berlin,'' which has made its way to the screen.

The movie, with Sean Penn and featuring Martin Sheen as Stern, is to be released in late March or early April.

The story is about what happened when Stern was sent to West Berlin in his capacity as a federal judge to preside at the trial of two East Germans charged with hijacking a Polish airliner to escape to the West.

The United States had jurisdiction over the case because the plane landed in the U.S. zone in Berlin. And though the case involved a dramatic flight to freedom, the State Department sought full prosecution of the hijackers to set an example for other nations being beseeched to crack down on plane piracy.

One of the hijackers was convicted, but Stern sentenced him to time already served, and the defendant walked out of the courtroom a free man.

In his book, Stern accused the State Department of pressing for a conviction to placate the Soviet Union and East Germany and trying its hardest to keep the defendants from getting a trial by jury.

The second-highest ranking U.S. official in Berlin argued against a jury trial, but Stern threatened to dismiss the case and free the defendants if they didn't get a jury trial, the judge said.

Stern is a graduate of New York City public schools, Hobart College and the University of Chicago Law School.

As a young prosecutor in the Manhattan District Attorney's office he specialized in homicide cases, including the one against suspects in the assassination of black activist Malcolm X.

As a lawyer for the Justice Department, Stern prosecuted defendants in a major kickback case involving New Jersey politicians and the operators of an interstate oil pipeline. One of the defense lawyers he defeated in that case, Frederick B. Lacey, became U.S. Attorney in New Jersey in 1969 and hired Stern as his top deputy.

Stern was nominally obeisant to the Republican Party in the early 1970s. At the same time, Stern often expressed pride in his independence.

Indeed, the publicity generated by his cases against cabinet members of Republican Gov. William T. Cahill between 1969 and 1973 were cited at the time as a major contributor to Cahill's defeat in 1973.