| Author |
Why does java not allow multiple public classes in a java file ?
|
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
|
Why should a public class be declared/kept in a java file with the same name as the class ?
|
SCJP 6. Learning more now.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Buddhika Mawella
Ranch Hand
Joined: Jan 06, 2011
Posts: 36
|
|
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.
|
BM
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
Please search; this question comes up quite frequently.
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
The Oak specification indicated it was "necessary for efficient package importation."
Ref: Java History 101: Once Upon an Oak.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
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.
|
 |
 |
|
|
subject: Why does java not allow multiple public classes in a java file ?
|
|
|