aspose file tools*
The moose likes Beginning Java and the fly likes Two classes with public access specifier in a single source file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Two classes with public access specifier in a single source file

Khuzema Dharwala
Ranch Hand

Joined: Apr 26, 2010
Posts: 47

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





OCJP 6
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Sep 26, 2008
Posts: 9
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
And welcome to JavaRanch
Khuzema Dharwala
Ranch Hand

Joined: Apr 26, 2010
Posts: 47

Thanks a ton guys
Nelson Sam
Ranch Hand

Joined: Jun 12, 2010
Posts: 30
What is access specifier by default for a class?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

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


[My Blog]
All roads lead to JavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two classes with public access specifier in a single source file