aspose file tools*
The moose likes Beginning Java and the fly likes Why only one public class in a .java file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why only one public class in a .java file?" Watch "Why only one public class in a .java file?" New topic
Author

Why only one public class in a .java file?

Anubhav act
Greenhorn

Joined: Jan 21, 2013
Posts: 2
Why only one public class is allowed in a .java file that too with the same name as of the file ??
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Anubhav act wrote:Why only one public class is allowed in a .java file that too with the same name as of the file ??

Because it is. And I'm afraid that, unless you can find one of the designers of Java, that's the best answer you're going to get.

If you want my opinion as to why: because it's simple. That has always been Java's general philosophy.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Imagine you have a source file that references a lot of classes. If public classes were allowed to be defined in a file without name restrictions, you would have to inspect every file in every package until you find the correct definition.

Right now the compiler only has to look for one file per package, and all files in the same package. That's a lot faster.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
Welcome to the Ranch
If you search this forum, you will find lots of other answers to the same question. Some of them might be helpful to you.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2394
    
  28

I remember back when Java was introduced, I used to program in Visual C++, and it was a pain to find classes in your source, or use some sort of naming convention. You had to basically use "Find all files" feature of WIndows (which was a pain too) if the project didn't have a naming convention. Nowadays, it doesn't matter because computers have grown powerful enough that IDEs can keep track of where all the classes are. Also back in the day, you had to compile your entire source code every time....and you liked it! Each .cpp file compiled into an .obj file, and then a linker would link all the files into an .exe file. The final link step had to execute every time. IDEs tried to be as lightweight as possible, because the compiler took so much goddammed memory.

Java introduced the rule that there can be only one class per java file, the folder structure of the source folder matched the package name exactly, and each java file compiles into couple of classes because it made sense for the IDEs at the time. When you opened a class, all the IDE had to do is use the package name to traverse the folder structure to find the file. When you changed the file, all it had to do is compile these one file into a class file without requiring an additional link step. This rediced the size of the compiler too.. because all the compiler has to do is load this one file and it's dependencies to compile the class. It doesn't need to load everything into memory. This led to more memory being available to the Editor.

Hmm now that I think about it, having a well defined rule for how source files are stored, lead to the copiler becoming lightweight, which lead to the IDEs having more features, which includes being able to keep track of where all the classes are stored, which lead to people asking "Why do we need a rule for how source files are stored?"
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Jayesh A Lalwani wrote:Java introduced the rule that there can be only one public or protected class per java file
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Stephan van Hulst wrote:
Jayesh A Lalwani wrote:Java introduced the rule that there can be only one public or protected class per java file

Ummm, I guess that's what Stephan wrote, not what Jayesh wrote. Stephan, can you show an example what you mean by "or protected" there? I don't think it's correct, unless you're talking about member classes, in which case private is also an option, and there's no limit on the number. Perhaps you meant there can only be one public top-level class? And any number of top-level package-protected classes?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Woops, I reasoned that Java's rule should be applicable to all top level classes exported in the API, but I forgot that protected is not a modifier that can be applied to top level classes!

Next time before I correct someone, I should probably look up a reference first.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why only one public class in a .java file?