• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why only one public class in a .java file?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why only one public class is allowed in a .java file that too with the same name as of the file ??
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic