• 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

one public class per source code file

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It is well proven fact that we can have only one public class per source code file. But I cannot understand what is the reason behind it to keep only one public class in a file. I want to know that why java developer have included this restriction in java laguage.

Regards
Gaurav
[ February 19, 2006: Message edited by: gaurav singhal ]
 
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
Personally, I like the idea of being able to find the source file I'm looking for.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Note: Some compilers might allow more than one public class per .java file. However, we recommend that you use the convention of one public class per file because it makes public classes easier to find and works for all compilers.


The above is found in the below link
http://java.sun.com/docs/books/tutorial/java/interpack/createpkgs.html

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.


The above is found in the below link
http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html
 
Murali Mohan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I personally didn't find any compiler that allows you to write two public classes in one source file.
 
marc weber
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
"Murali,"

Thanks for the above posts!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention!
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Putting a slight twist on the question. Is it common practice to include multiple classes (obviously not public) within one source file? It likely makes sense if the classes are related or you want to have all of your classes in a given package inside one file but other than that, I don't see any advantage. Is there something I am missing?
 
gaurav singhal
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks chris that answer my question on it.
But still as such I dont see any problem if they allow more than one public class in a file.

There can be little complexity in term of finding the name of the class and file but that is there when we dont have any public class in a file.
In other words java allow us to to give any name to source file if we dont have public class in a file.

According to my understanding it can be a Java Convention to name a file with any of the name of the class present in a file rather than restricting it to name the file according to public class present in it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic