File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why one public class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why one public class " Watch "Why one public class " New topic
Author

Why one public class

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Hi,
Why does java restrict for one public class per one file.
Thanks


Groovy
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
i guess it is similar as single inheritance.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It has to do with auto-compiling. If you use a class that has not been compiled yet, Java will look for the matching .java file and compile it for you. If the class isn't in a .java file, java has no way to locate it.
This goes back to the Oak days. This is from the Oak documentation: "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."


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Thanks! Thomas.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

What about non-public class Thomas?
I can have a public class A in a file A.java along with class C (non public). Now I am using class C in another class Test. How does the auto compling work here now?

Originally posted by Thomas Paul:
It has to do with auto-compiling. If you use a class that has not been compiled yet, Java will look for the matching .java file and compile it for you. If the class isn't in a .java file, java has no way to locate it.
This goes back to the Oak days. This is from the Oak documentation: "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."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[This was composed when there were only two posts in this thread]
I think it's to make things easier for compilers (and humans) to find classes when they are referenced. Having only one public class in a file allows the additional restriction that the file name must match the class name. This means whenever a class in another package is mentioned, the compiler can quickly find the file containing that class, without having to seach all the files in the directory for that package. Actually, depending on the classpath there may well be more than one directory that can be associated with a given package (different roots) and of course these directories may be in jar files rather than "real" directories. But the general point about making it easier to find classes remains the same.
So, why do they allow additional non-public classes inside a file? It does increase the number of places the compiler needs to search - every file in the current directory, or in corresponding directories elsewhere in the classpath. I think it was an attempt to make Java simpler to learn. It allows people to make programs involving multiple classes before they understand packages or even access modifiers - just put all the classes in the same file, and you can omit all "public" declarations (unless you're overriding something else), and need no "package" declaration. Gives you a few less things to worry about...
[ May 06, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Pradeep Bhat:
What about non-public class Thomas?
I can have a public class A in a file A.java along with class C (non public). Now I am using class C in another class Test. How does the auto compling work here now?


It doesn't. In this case the default access class can not be found and you must pre-compile it yourself.
 
Consider Paul's rocket mass heater.
 
subject: Why one public class
 
Similar Threads
Why should there be only one public class per source file
public class
source file?
java concepts
reg public class in a java source code