Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why one public class

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Why does java restrict for one public class per one file.
Thanks
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i guess it is similar as single inheritance.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Thomas.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic