There are dozens of rules surrounding what the royal family can and can't do. For senior royals — that is, ones closest to the throne — their full-time job is being royalty and carrying out duties related to their status, whereas less significant royals often have "real" world jobs alongside some charity work. For instance, the recently-married Princess Eugenie is a director of an art gallery, and her cousin Peter Phillips works in sports management and sponsorships. There's one profession, thought, that even direct heirs are allowed to have for a while: the military.
Not only is military service permitted for members of the royal family, it's actually expected of the highest-ranked men. This is a tradition dating back centuries and continued to this day, albeit with slight adjustments in the modern day. The Queen's three sons — Prince Charles, Prince Andrew, and Prince Edward — all had some military service, and Prince William and Prince Harry also served. That's why, for instance, both William and Harry were married in dress uniforms, rather than traditional suits.
The branches of the military can vary depending on the individual's interests. Charles and Andrew both had naval careers, like their father Prince Philip, while William served in the Royal Air Force (and later became a civilian air ambulance pilot) and Harry went into the army before becoming a helicopter pilot. Active service is encouraged, and some royals have actually served in dangerous war zones. Prince Andrew served during the Falklands War, and more recently, Prince Harry was secretly deployed to Afghanistan in 2007 and again in 2012, despite his royal status making him a particular target.
Royal military service hasn't always been without its snags, though. Prince Edward, the Queen's youngest son, caused a furore when he dropped out of training for the Royal Marines after completing just a fourth of his training. Because the Marines had helped pay for his University education on condition of future service, there was a considerable uproar. For the most part, though, royal military service has been a longstanding and proud tradition that gives the royal family another way to serve their country.