• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why does java not allow multiple public classes in a java file ?

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why should a public class be declared/kept in a java file with the same name as the class ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just because that's how the designers of the Java language designed it. They probably thought that would make it easier to find the source of a specific class - if you know the class name, you know for what file to look. There's no technical reason why this would be necessary.
 
Ranch Hand
Posts: 40
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not even public class, if you define a public interface or a enum in java, you should give them a dedicated file with the same name of the interface or enum.
 
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please search; this question comes up quite frequently.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Oak specification indicated it was "necessary for efficient package importation."

Ref: Java History 101: Once Upon an Oak.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

marc weber wrote:The Oak specification indicated it was "necessary for efficient package importation."

Ref: Java History 101: Once Upon an Oak.



Thanks for the link. I visited it and saw this : "The compiler would have to make an additional pass through all the compilation units (.java files) to figure out which classes were where, which would make the compilation even slower. "

The meaning of "...additional pass through all..." is not clear. Can you tell me what it means ?
 
Campbell Ritchie
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a particular class is not found in the current file, the compiler would have to open and parse every .java file in the directory to find it.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic