Bookmark Topic Watch Topic
  • New Topic

Two classes with public access specifier in a single source file

 
Khuzema Dharwala
Ranch Hand
Posts: 51
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • 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



 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • 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.
 
XinShou ShangLu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Report post to moderator
And welcome to JavaRanch
 
Khuzema Dharwala
Ranch Hand
Posts: 51
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks a ton guys
 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Report post to moderator
What is access specifier by default for a class?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • 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".
 
    Bookmark Topic Watch Topic
  • New Topic