(Health.com) -- Children who live with dogs and cats are less likely to develop allergies to those animals later in life, but only if the pet is under the same roof while the child is still an infant, a new study suggests.

Compared to babies born into cat-free homes, those who grew up with cats were roughly half as likely to be allergic to them as teenagers, the study found.

Growing up around a dog reduced the risk of dog allergies by about the same amount for boys, but not for girls -- a finding that mystified researchers.

Being exposed to pets anytime after the first year of life appeared to have no effect on allergy risk, however, which indicates that timing may be everything when it comes to preventing allergies.

Health.com: 15 hypoallergenic dogs and cats

Though they can't say for sure, the researchers suspect that early exposure to pet allergens and pet-related bacteria strengthens the immune system, accustoms the body to allergens, and helps the child build up a natural immunity.

"Dirt is good," says lead researcher Ganesa Wegienka, Ph.D., summing up the theory. "Your immune system, if it's busy with exposures early on, stays away from the allergic immune profile."

This isn't the first study to find that having a household pet may protect kids from allergies, but it is the first to follow children until they were 18 years old.

Health.com: How to reduce pet allergens at home

Previous studies have had mixed results -- some have even linked pet exposure during infancy to an increased risk of allergy -- so it's too early to recommend getting a dog or cat just to ward off allergies in your infant, says David Nash, M.D., clinical director of allergy and immunology at the Children's Hospital of Pittsburgh.

"In the end, we'll probably find out that there are periods of opportunity when exposure to allergens, for some people, is going to have a protective effect," says Dr. Nash, who was not involved with the new study. "But we're a long way from figuring out who it's protective for and when that optimal period is."

By the same token, don't give away your beloved family pet because you're concerned the critter will provoke allergies.

"I would not get rid of my dog if I was having a child," says Wegienka, an epidemiologist in the department of public health sciences at Henry Ford Hospital, in Detroit. "There's no evidence that you should get rid of a dog or a cat."

Health.com: What to do if you're allergic to your pet

Moreover, it's possible that factors other than having a dog or cat in the house influenced the study participants' risk of allergy.

For instance, although the researchers took into account whether the children's parents were allergic to animals, they didn't ask about a broader family history of allergies or other health problems. So it could be that children who are genetically predisposed to animal allergies simply are less likely to grow up in homes with pets.

In the study, which appears in the journal Clinical & Experimental Allergy, Wegienka and her colleagues collected information from 566 children and their parents about the kids' exposure to indoor pets and their history of allergies.

In addition, when the kids turned 18, the researchers took blood samples and tested them for certain immune-system proteins (known as antibodies) that fight off cat and dog allergens.

The teenagers who lived with a cat during their first year of life had a 48 percent lower risk of cat allergy than their peers, and the teen boys who lived with a dog had a 50 percent lower risk of allergy.

The authors suggest that infant girls may not develop the same immunity as boys because they may interact differently with dogs than infant boys, but that's only a guess.

Copyright Health Magazine 2011