File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes why a class must be named in the name of public class in a source file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why a class must be named in the name of public class in a source file?" Watch "why a class must be named in the name of public class in a source file?" New topic
Author

why a class must be named in the name of public class in a source file?

Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
public class A { }
public class B{public static void main(String args[]){ } } saved as B.java

when i compile the file iam getting error.
can anyone tell me why a source file cannot have more than one public class
and why the source file must be saved in the name of "public class name".


Abhishek
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
If you search this forum, you should get plenty of discussions on this topic.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
i believe you would save it as A.java, but have B as a subclass of A

Justin


You down with OOP? Yeah you know me!
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Originally posted by Justin Fox:
i believe you would save it as A.java, but have B as a subclass of A

Justin


no, you would not. The language specs state quite explicitly that you can AT MOST ONE public class in a sourcefile and that if ther is such a public class the sourcefile MUST have the name of that class.


42
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

The Specification says that, ( ofcoure OO principle too), class, should expose only one externel interface ( API ) ( one entry & one exit), so one Public class per one source file.

Explore more ....
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You can, though, have as many public nested and inner classes as you like.

You shouldn't use this to bypass the "one public class per file" rule - you should use nested or inner classes only when they really are strongly related to the enclosing class. I think public nested or inner classes are only occasionally a good idea. In contrast, private ones are often useful.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The old Oak 0.2 specification appears to suggest this was a performance optimization...
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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
There can be at most one public class in a source and the name of the class should be the same as the source file.


Rahul Bhattacharjee
LinkedIn - Blog
 
wood burning stoves
 
subject: why a class must be named in the name of public class in a source file?
 
Similar Threads
Naming a java file?
A very simple question (Please Answer)
only 1 public class per source file
Source File declaration rule in java