FRANKFURT, West Germany (AP) _ Confessed TWA hijacker Mohammed Ali Hamadi said Wednesday that his accomplice shot and killed a U.S. Navy diver aboard the plane because he was afraid the jetliner was going to be attacked.

Hamadi was commenting on testimony by co-pilot Philip G. Maresca, who earlier told the court Hamadi came into the cockpit carrying a gun seconds after 23-year-old sailor Robert Stethem was killed.

Hamadi, a Lebanese Shiite Moslem, is being tried on murder and air piracy charges in the June 1985 hijacking during which 39 Americans were held hostage for 17 days.

He has admitted taking part in the hijacking but denied killing Stethem.

The defendant disputed most of Maresca's testimony and again blamed his accomplice, Hassan Ezzeddine, for killing Stethem. Ezzeddine is a fugitive.

Hamadi said armed men surrounded the plane in Beirut. He said Ezzeddine fired two warning shots from the cockpit window as they tried to approach.

He said Ezzeddine earlier had told the tower he wanted to speak to a certain person and became ''very agitated'' when that individual did not show up.

''He came back and took Stethem. I spoke with him about it, but he was determined that one of the military people had to be shot. He was afraid of an attack on the plane,'' Hamadi said.

Chief Judge Heiner Mueckenberger asked who was it that Ezzeddine wanted to speak to and Hamadi replied: ''He was the one responsible for the hijacking and he was from Amal (the Shiites' main military arm in Lebanon).''

The judge asked if he was willing to identify the person and Hamadi replied: ''No, not at this moment.''

Hamadi denied going into the cockpit after Stethem was shot.

Maresca told the court, ''I stand by my testimony.''

The judge then adjourned the trial until Monday.

Maresca testified regarding the circumstances of Stethem's shooting:

''I was able to peek out of the corner of my eye and I thought I was able to determine that a passenger ... was being dragged to the front of the plane.

''Then I heard a gunshot. ... About 10 to 15 seconds later, Hamadi came into the cockpit with a gun in his hand.

''Then Uli Derickson (the flight purser) came in very distressed and said one of the passengers had been killed.''

He fought back tears as he described the scene.

The diver's parents, Richard and Patricia Stethem, listened quietly during the testimony. They have attended the trial regularly since it began on July 5.

Maresca, 46, said he did not see the shooting because Hamadi ordered the crew ''to look forward and not behind and not to pay attention to any noise.''

The co-pilot said the hijackers beat the crew every time they tried to establish radio contact with other aircraft or with airports.

Maresca said he felt from Hamedi's actions that Hamadi was the leader of the two hijackers.

''He was more volatile, very excitable. The other hijacker tried to restrain Hamadi when he was extremely abusive. Hamadi had the gun most of the time and he made most of the demands on us,'' Maresca said.

The judge granted a defense motion for a psychiatric examination. The defense says Hamadi was mentally disturbed at the time of the hijacking.

Hamadi was arrested Jan. 13, 1987 at Frankfurt airport after customs officials found liquid explosives in his luggage.