BOSTON — Jaime Caetano was beaten so badly by her ex-boyfriend that she ended up in the hospital. So when a friend offered her a stun gun to protect herself, she took it.

Caetano, who is homeless, never had to use it but now finds herself at the center of a contentious Second Amendment case headed to the highest court in Massachusetts.

The Supreme Judicial Court is being asked to decide whether a state law that prohibits private citizens from possessing stun guns infringes on their right to keep and bear arms. In an unusual twist, the court is also being asked to examine whether the Second Amendment right to defend yourself in your own home applies in the case of a homeless person.

Arguments before the court are scheduled Tuesday.

Police found Caetano’s stun gun in her purse during a shoplifting investigation at a supermarket in 2011. She told police she needed it to defend herself against her violent ex-boyfriend, against whom she had obtained multiple restraining orders.

During her trial, Caetano, 32, testified that her ex-boyfriend repeatedly came to her workplace and threatened her. One night, she showed him the stun gun and he “got scared and left me alone,” she said.

She was found guilty of violating the state law that bans private possession of stun guns, devices that deliver an electric shock when pressed against an attacker.

In her appeal, her lawyer, Benjamin Keehn, argues that a stun gun falls within the meaning of “arms” under the Second Amendment. Keehn wrote in a legal brief that the state’s ban “cannot be squared with the fundamental right to keep and bear arms.” He also argues that self-defense outside the home is part of the core right provided by the Second Amendment.

Massachusetts is among only five states that ban stun guns and Tasers for private citizens, said Eugene Volokh, a constitutional law professor at the University of California, Los Angeles, who has written extensively about Second Amendment issues. The devices are used by law enforcement agencies around the country.

A ban in Michigan was overturned in 2012 after the state appeals court ruled that a total prohibition was unconstitutional under the Second Amendment and the Michigan Constitution.

Volokh said the Second Amendment right to keep and bear arms for self-defense “necessarily includes the right to do so outside one’s home.”

“Given that the Second Amendment secures a right aimed in large part at self-defense, we have to recognize that often people need to defend themselves in public,” Volokh said. “So it’s especially important that people have some useful device that would be effective in defending themselves outside the home.”

Commonwealth Second Amendment, a Massachusetts-based group that filed a friend-of-the-court brief in the case, argued that stun guns are a “logical option” for people who do not want to use deadly force for self-defense.

“Let’s face it, not everybody who has a need for self-defense would necessarily be comfortable carrying a firearm,” said Brent Carlton, the group’s president and co-founder.

Middlesex District Attorney Marian Ryan declined to comment on the case.

In a legal brief, prosecutors argue that the Second Amendment does not establish a constitutional right to own a stun gun and that two pivotal U.S. Supreme Court decisions that upheld the right to own a firearm for self-defense inside homes did not automatically grant that right outside the home.

“While homelessness is a serious problem all its own, the defendant has offered no authority to support the idea that a person without a home may carry a firearm wherever he or she may travel or otherwise exercise a Second Amendment right free from the limits that restrict that right for every other person otherwise situated,” Assistant District Attorney Michael Kaneb wrote.