The CJEU, Europe's highest court, had been asked to rule on a dispute between the Basque Police and Emergency Services Academy in Spain and an applicant for a role.
The Academy had stated that applicants should be under 35 years of age, but the applicant argued that this was discriminatory as there were no reasonable grounds for the restriction.
In its judgment the CJEU said that under the EU directive on equal treatment in employment and occupation "a difference of treatment based on age is not to be regarded as discrimination where a characteristic related to age, such as the possession of particular physical capacities, constitutes a genuine and determining occupational requirement".
The duties of a police officer relating to protecting people and property, arresting and guarding offenders and preventative patrolling may require "a particular physical capability in so far as physical inadequacies in the exercise of those duties may have significant consequences not only for the police officers themselves and third parties but also for the maintenance of public order", the CJEU said.
The rank for which the competition was organised is not involved in administrative tasks, the Court said. A separate recruitment process, involving no age limit, is used for that type of work, it said.
The Court also said that the average age of the police force is significantly rising, and it is therefore essential to plan for the replacement of older officers by recruiting younger staff. A young police officer is able to perform physically demanding tasks more effectively, it said, and "with a view to re-establishing a satisfactory age pyramid, the possession of particular physical capacities should be envisaged dynamically, taking into consideration the years of service that can be accomplished by a police officer after he or she has been recruited".
Employment law expert Linda Jones of Pinsent Masons, the law firm behind Out-Law.com said: "This is a surprising decision, given that it appears to be based on an assumption that people over 35 are less fit and it may open the door for other employers to impose age limits when recruiting into roles involving a high degree of physical work."