In java, why is it mandatory for each class to declare the package it belongs to. I mean, package hierachy is already described/enforced using the folder structure of the file system. It looks to me that the line:
package com.mypackage;
As the first line of each class is somehow a redundant information. But I'm pretty sure there's a good reason for it to be there.
I mean, package hierachy is already described/enforced using the folder structure of the file system.
Not necessarily. While it's certainly a good idea to organize your code that way, it's not a requirement. Also bear in mind that source code needn't be in a file system to start with.
Fundamentally, I think it's entirely reasonable to make the content of the source file the important thing. Heck, you could take it to the other extreme and assume the class being declared by the filename - that's not just a convention, but one which is explicitly valid (in the JLS) to be enforced by the compiler for public top-level classes.
See more on this question at Stackoverflow