aspose file tools*
The moose likes Beginning Java and the fly likes A very simple question (Please Answer) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A very simple question (Please Answer)" Watch "A very simple question (Please Answer)" New topic
Author

A very simple question (Please Answer)

Pattu Babu
Greenhorn

Joined: May 25, 2005
Posts: 2
Why there is a restriction that, if a public class is there inside a source file, then the source file name has to be the same as the public class name.


Raihana
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Rules had to be made for how the javac program would function. Rule number one was only one public class per .java file.
[ May 25, 2005: Message edited by: Nigel Browne ]
Pattu Babu
Greenhorn

Joined: May 25, 2005
Posts: 2
Nigel Browne,
But, why such a confusion for the compiler in case of publc classes alone and not for the default classes.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I'm afraid that the answer remains "because those are the rules". What requirement do you have which needs two public class definitions in the one file?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
It is perfectly legal to write the following:
You could then compile and run this class

You can also do the following: You can then save this file as Hello.java and compile and run each of the two Test classes, but you can't have two public classes in one .java file because that is the rule and the compiler will not let you get away with it.
[ May 25, 2005: Message edited by: Nigel Browne ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's a convenience for the compiler. If you reference a class called BowlingGame the compiler knows to look for the source in a file called BowlingGame.java and the compiled code in a file called BowlingGame.class. If not for that rule the compiler might have to look inside all the files in the current directory or your whole classpath to find the one that says "class BowlingGame". Oh, and it's more convenient for us humans, too!
[ May 25, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A very simple question (Please Answer)