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

Two classes with public access specifier in a single source file

 
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi All,
I just wanted to know why there shouldn't be two classes with "public" access specifier in a single java source file . Any kind of guidance is highly appreciable ......... Thanks



 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Why? Because it made things simpler for the authors of the first Java compiler. Only public classes are visible outside of their package; therefore, under some circumstances you'll need to easily find the source for a class in another package. If that class is guaranteed to be in a file named after the class, the task becomes trivial.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
It also makes it simpler for humans looking at the source code, to find where things are defined. Nowadays, most of us would use a good IDE and this becomes a non-issue. But there were no such nice IDEs for Java in the early days. And even now, sometimes you're looking at code on a server where you don't have an IDE installed. It's nice to just know where the code should be, rather than having to load the code on a different machine before you look at it.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
If there is a class with 'public' access modifier in one java source file, the java source file must be named as the same as the class name.
Why? I think as Ernest and Mike said, and it is easy for some IDE to match the source file and class file by same name.
So there cannot be two 'public' class in one java source file cause of it cannot be two names for one file.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
And welcome to JavaRanch
 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks a ton guys
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What is access specifier by default for a class?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Nelson Sam wrote:What is access specifier by default for a class?


Can you start a new thread instead of hijacking this one ?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Christophe Verré wrote:

Nelson Sam wrote:What is access specifier by default for a class?


Can you start a new thread instead of hijacking this one ?

Thank you for noticing. Since this thread appears answered, I shall close it.

And it's not "access specifier", but "access modifier".
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic