| 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: 6588
|
|
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 - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
|
|
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
|
Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
|
 |
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
|
 |
 |
|
|
subject: source file?
|
|
|