• 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

Rules regarding Public Classes

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm new to this forum and quite new to Java as well. I've finished studying the Language Fundamentals for the SCJP exam and when learning about public classes I got 2 problems:

1. What is the reason for allowing only ONE public class per source file?
2. Why does the source file HAVE to be named with the public class's name?

Thanks alot
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. What is the reason for allowing only ONE public class per source file?
That is the rule Java have. So just follow it.

2. Why does the source file HAVE to be named with the public class's name?
That is the rule Java have. So just follow it.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have a look at the Javaranch's naming policy.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Aaron Raja]1. What is the reason for allowing only ONE public class per source file?
That is the rule Java have. So just follow it.


Well to be precise it's the compiler's rule. Generally compilers enforce such a rule. The java language specification doesn't mandate that a public class should be in it's own file. But till date I haven't heard of any compiler that does not follow this convention.
[ June 20, 2007: Message edited by: Anupam Sinha ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Section 7.6 of Java Language Specification 3.0 talks a little about this.

http://java.sun.com/docs/books/jls/download/langspec-3.0.pdf

Might help.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello "nelumd"-

Welcome to JavaRanch.

On your way in you may have missed that we have a JavaRanch Naming Policy for displayed (screen) names. Your displayed name must consist of a first name (or an initial), a space, and a family name (in that order) and not be obviously fictitious. Since yours "nelumd", does not conform with it, please take a moment to change it, which you can do right here.

Posters with nonconforming displayed names will be locked out of JavaRanch after a few posts using those names.

Thanks
-Barry

(NR) - search tag
 
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

Originally posted by Rahul Bhattacharjee:
Section 7.6 of Java Language Specification 3.0 talks a little about this...


Here's a link (if you don't want to download the entire PDF): JLS - 7.6 Top Level Type Declarations.

However, I don't see much here about why. The Oak 0.2 specification appears to suggest this was a performance optimization. ("Oak" was the original name for "Java.")

Although each Oak compilation unit can contain multiple classes or interfaces, at most one class or interface per compilation unit can be public... This restriction is ... necessary for efficient package importation.


Ref: Once Upon an Oak.
 
May De Silva
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Thank you for all the replies. I also read "JavaRanch Naming Policy", "Ask a Meaningful Question", and "HowToAskQuestionsOnJavaRanch" which made me realize some of my mistakes and learn alot out of them. Just want to appreciate all the people behind this site, because it's really good. Thank you so much for being patient with us greenhorns and for giving us the opportunity to improve and for saying it as it is.

God Bless You
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic