Why Many Years on the Same Job Can Hurt Your Skills
Spending many years on the same job leads to loss of skills because when you are doing routine tasks, this makes you forget all the other things you once knew. Even jobs that require diverse skills, can't put all your skills to use. For instance, if you are a Java developer and you write only enterprise applications, you will hardly follow the recent developments in mobile Java, for example.
After a couple of years as a Java enterprise developer, Java enterprise development will be the only area you will know because you will have missed all the developments in other areas. Of course, you can keep an eye on them but it is very different when you just read the news about a new technology from when you write real-world applications.
At some point, when you decide to wave goodbye to your co-workers in Java enterprise development and try to switch to another area, even if it is still a Java development variety, you discover that things have changed so much that you actually need to start from scratch. When you are 40 or 50, have a family to support and a mortgage to pay, starting from scratch is barely your dream career movement.
Basically, these are the risks of narrow specialization and they are present in any profession. Sure, you can't change jobs twice a month just to keep your skills sharp, but when you spend many years on the same job, this leads to loss of skills and you become less competitive on the market.
This is why it is considered that 3 to 4 years on the same job is optimal because you don't lose your skills and the employer doesn't have an employee who is light years behind the current state of things.