Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

why allow multiple classes in one file?

Thread view: 
Hilbert - 15 Jul 2006 19:23 GMT
Hello,

I'm pretty new to Java, so please don't yell at me if my question is
dumb.

Each public file should be in a separate file. I don't understand why
is it allowed for a file contain additional non-public (default)
classes. Why is it not one class per file, public or default?

Thanks,
Hilbert
Chris - 15 Jul 2006 19:39 GMT
> Hello,
>
[quoted text clipped - 7 lines]
> Thanks,
> Hilbert

Sometimes it's useful to have a very small utility class that is used
solely within a larger class. They're so tightly coupled that it's
convenient to have them together in one file.

Sometimes you don't want to implement the utility class as an inner
class because inner classes can cause problems. Some obfuscators, for
example, don't handle them very well.

I would not put two large classes together in one file, nor would I
include a secondary class in a file if it were used anywhere outside
that file.
Adam Maass - 16 Jul 2006 00:26 GMT
>> Hello,
>>
[quoted text clipped - 19 lines]
> include a secondary class in a file if it were used anywhere outside that
> file.

I agree with you (mostly), but must point out the history:

The capability for multiple classes in one source file dates all the way
back to Java 1.0, (IE, to before inner classes). In 1.0, a small utility
class used only by one other class in the same package could be handled by
putting it in the same source file as that class.

Since the introduction of inner classes, this really (really) should be
handled as a static class member of the one class that uses it.

-- Adam Maass
Hilbert - 16 Jul 2006 14:19 GMT
Thanks for the info!

Thanks,
Hilbert

> >> Hello,
> >>
[quoted text clipped - 31 lines]
>
> -- Adam Maass


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.