• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

more than one public class in a single file

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80254
428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80254
428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am afraid I understand that even less.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic