Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

more than one public class in a single file

 
akhil lalwani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12180
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49729
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49729
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid I understand that even less.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic