This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you search this forum, you should get plenty of discussions on this topic.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i believe you would save it as A.java, but have B as a subclass of A

Justin
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic