• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 13089
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: 79063
376
  • 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: 79063
376
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic