A Navy officer who formerly taught school in Prince William County was fatally stabbed in the back late Sunday while his ship was on a training cruise near Bermuda, the Navy said yesterday.

Lt. James K. Sterner, 35, an engineering officer aboard the frigate USS Miller, was discovered lying near the ship's engine rooms, an Atlantic Fleet spokesman said in Norfolk. Sterner died about 40 minutes later after a hospital corpsman tried to revive him.

The spokesman, Lt. Cmdr. Ken Satterfield, said the Miller was due back in its home port of Newport, R.I., late today and would be anchored offshore. A Navy official who asked not to be identified said the crew would not be allowed ashore until naval investigators made an arrest.

Sterner taught languages, arts and social studies at the middle-school level for three years in Prince William County and left in 1980, school officials said. The Navy said he had listed Woodbridge as his home.

A 1973 graduate of Duquesne University, Sterner joined the Navy in 1980 in Washington and was commissioned an ensign Oct. 1, 1981, at the officer candidate school in Newport.

He was stationed in Newport, where he lived with his wife Candy and their three children, the youngest of whom is 1 1/2 months old.

Satterfield said few details of the slaying were immediately available because the ship was at sea. He said the stabbing occurred about 10:40 p.m., but he declined to say whether a weapon had been recovered. The ship has no history of similar troubles and recently received outstanding grades on an engineering inspection, Satterfield said.

The Miller left Newport June 3 for operations in the western Atlantic and was scheduled to return July 4. It headed home yesterday instead, Satterfield said, to allow Naval Investigative Service personnel to begin probing the slaying.

The 4,100-ton, 438-foot Knox-class frigate, commissioned in 1973, is used to train naval reservists. Sterner was a member of the ship's small, permanent crew, the Navy said.

Satterfield said Sterner was the Miller's main propulsion assistant, in charge of a number of men who work in the engineering rooms.