The Right Rev. John T. Walker, Episcopal bishop of Washington, told 3,300 University of Maryland graduates yesterday that nonviolent civil disobedience on the part of campus and church protesters is justified when all other efforts to bring change fail.

"Such was the case in the civil rights movement," he said in a commencement address at the College Park campus. "Such also seems to be the case in the antiapartheid movement." University of Maryland students recently created a "shantytown" to protest South African policies limiting the rights of its black citizens.

Walker urged the graduates to "help protect the principle of freedom of religion . . . and the inherent right of religious bodies" to press for government responsibility and restraint, particularly in matters of war, peace and human rights.

The bishop, who was arrested last year outside the South African Embassy for protesting apartheid, made no mention of the violence that has erupted on some college campuses because of intense student feelings on apartheid.

At Johns Hopkins University in Baltimore, for instance, a student-built shantytown was firebombed Friday night, injuring one occupant. A student, Russell H. Abrams, has been charged with arson and attempted murder.

The bishop did note, however, that those who protest South African apartheid face a variety of criticisms.

Those who protest against American support of South African President Pieter W. Botha are said by some to be "at worst a dupe of black communists . . . and at best misguided church leaders or young people who do not know what is going on," Walker said.

"If you call for economic sanctions," he said, "the riposte is this: 'In South Africa, they won't work, they will be ineffectual.' On the other hand, by magic, they will work in Nicaragua."

Walker said that those who continue to question U.S. government policies "are told that the government has information about Central America that you don't have or again that you are dupes of communism." If the church persists, he said, "we are threatened again with the loss of tax exemption for engaging in political activity."

The bishop said that the U.S. government is selective in its sanctions of political protest in this country. "Demonstrators against the invaders of Afghanistan are not considered political activity, nor are demonstrations at the Soviet Embassy against the treatment of dissidents in that country," he said.

"I am not opposed to demonstrations at the Russian Embassy," Walker said. "I simply raise the question about the threats to religious freedom posed by the government's right to tax" organizations that engage in political activity. Church groups whose activities are considered to be political run the risk of losing their tax-exempt status, he said.