Two classes with public access specifier in a single source file
Khuzema Dharwala
Greenhorn
Joined: Apr 26, 2010
Posts: 6
posted
0
Hi All,
I just wanted to know why there shouldn't be two classes with "public" access specifier in a single java source file . Any kind of guidance is highly appreciable ......... Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal
Why? Because it made things simpler for the authors of the first Java compiler. Only public classes are visible outside of their package; therefore, under some circumstances you'll need to easily find the source for a class in another package. If that class is guaranteed to be in a file named after the class, the task becomes trivial.
It also makes it simpler for humans looking at the source code, to find where things are defined. Nowadays, most of us would use a good IDE and this becomes a non-issue. But there were no such nice IDEs for Java in the early days. And even now, sometimes you're looking at code on a server where you don't have an IDE installed. It's nice to just know where the code should be, rather than having to load the code on a different machine before you look at it.
XinShou ShangLu
Greenhorn
Joined: Sep 26, 2008
Posts: 9
posted
0
If there is a class with 'public' access modifier in one java source file, the java source file must be named as the same as the class name.
Why? I think as Ernest and Mike said, and it is easy for some IDE to match the source file and class file by same name.
So there cannot be two 'public' class in one java source file cause of it cannot be two names for one file.