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 more than one public class in a single file 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 "more than one public class in a single file" Watch "more than one public class in a single file" New topic
Author

more than one public class in a single file

akhil lalwani
Greenhorn

Joined: Aug 21, 2010
Posts: 5
Why should a source code file have only one public class if after compilation, each class has a separate byte code file?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Welcome to JavaRanch!

Because that's the way it works--that might not be a particularly satisfying answer, that's just the way it is. The class file definition allows for only a single class per file. But for tightly-coupled classes it can make more sense on an organizational level to keep them in the same file, and you may not want to expose it directly for some reason.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

David Newton wrote:Welcome to JavaRanch!

Because that's the way it works--that might not be a particularly satisfying answer, that's just the way it is. The class file definition allows for only a single public class per file. But for tightly-coupled classes it can make more sense on an organizational level to keep them in the same file, and you may not want to expose it directly for some reason.

fixed it for ya


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

And also the if you have public class then the name of the file should be same while running the code,
while compile you can give any name but it will create a class file of the name of class which is declared public.

we can also say that as a good designer we must have to show only a single class outside the package and try to do all the work from that class file(which will be difficult) and that class should be public.


SCJP6.0,My blog Ranchers from Delhi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
shanky sohar wrote: . . . must have to show only a single class outside the package and try to do all the work from that class file(which will be difficult) and that class should be public.
Are you sure about that? I don't really understand that bit.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

fred rosenberger wrote:
David Newton wrote:The class file definition allows for only a single public class per file.
fixed it for ya

No, you actually broke it--the class file definition allows only a single interface or class per file. If I had said *source* file, I'd agree.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Campbell Ritchie wrote:
shanky sohar wrote: . . . must have to show only a single class outside the package and try to do all the work from that class file(which will be difficult) and that class should be public.
Are you sure about that? I don't really understand that bit.


it is not possible.that complete behavior of a package will depend on a single class.I am saying its very difficult.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
I am afraid I understand that even less.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: more than one public class in a single file