aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "source file?" Watch "source file?" New topic
Author

source file?

Palash Kumar
Ranch Hand

Joined: Jul 21, 2009
Posts: 33

There can be only one public class per source code file.

WHY....???


SCJP 6 with 80%, going for SCWCD
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Why would you want to use more ?

First of all, you can create public inner classes (even static that do not share any relationship with the enclosing class.

As for fully-fledged 'Top-Level-Classes', it is possible to only have one public class per file).

Why?

First of all it is always better in terms of source control to have each class in a separate file.
Imagine you put 5 classes into one file, it quickly becomes harder to still have the picture of your project.
There are, of course, exceptions. If there is a small helper class that's sole purpose is to do a little thing in the enclosing class, then an InnerClass or Anonymous class would just do fine.

Second of all, more than one class per file ultimately results in more code. Which is not what you want due to code maintainability and the concept of cohesion.

Hence it might be that the architects of Java simply have decided to restrict java files to have one Top-Level-Class per source file.


JDBCSupport - An easy to use, light-weight JDBC framework -
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Palash Kumar wrote:There can be only one public class per source code file.

WHY....???


If you search the forum you will find that people have tried to answer this question before


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

The source file name must match the name of the key class in the source file. With two public classes, what would the file be named?

-Cameron McKenzie
Palash Kumar
Ranch Hand

Joined: Jul 21, 2009
Posts: 33

why it is necessary to name the file as of public class.....?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
without really knowing, I think it's for the ClassLoader.

Imagine you have a package with 46 classes in it.

then you instantiate new FancyClass();

Without doing a lot of research the ClassLoader knows that he is looking for FancyClass.class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: source file?