Major oil companies in Maryland suffered their second major defeat in a week when a federal judge here ruled yesterday that the state law banning conversion of full-service gas stations to gas-only operations is constitutional.

Edward S. Northrop, chief judge of the U.S. District Court here, said in his decision that "the overriding local interest in this case is the preservation of motorist safety on the Maryland highways.

"The state's concern is that emergency and normal automotive repair facilities will not be readily available if many more full-service stations convert to gas-only operations," Northrop added.

Lawyers and spokesmen for the Sun Oil Co. of Pennsylvania, which challenged the conversion ban, would not say whether the decision would affect gas prices. They contended, as they had in their legal arguments, that the state law restricts competition and prevents the company from offering a choice of services to consumers.

Tom Wilson, a lawyer in the state attorney general's antitrust division, defended the state law, saying: "It's true consumers do get a good deal at gas-and-go stations. But why do they (oil companies) sell gas wholesale to the full service dealers at the same price they charge retail at gas-and-go stations? There's no way in the world a dealer can sell gas in competition if he has to buy gas at 55 cents per gallon . . . when the company is retailing gas at other stations for 55 cents per gallon."

Last week the U.S. Supreme Court upheld another Maryland law that prohibits operation of service stations by major oil companies and out-of-state producers and refiners.

Wilson said the major oil companies had challenged both laws because they wanted to force the full-service dealers who rent stations from them out of businesss in order to cut their real estate holdings while concentrating on selling a great deal of gasoline from their own outlets.

Wilson said the two-year conversion ban which took effect July 1, 1977, had been aimed at the dealers rather than the oil companies.

"The legislature was concerned about losing the number of full-service stations we had," Wilson said. "There's nothing that stops them [the oil companies] from going out tommorrow and building one million new stations, all gas-and-go."