why allow multiple classes in one file?  
Author Message
Hilbert





PostPosted: 2006-7-16 2:23:00 Top

java-programmer, why allow multiple classes in one file? 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





PostPosted: 2006-7-16 2:40:00 Top

java-programmer >> why allow multiple classes in one file? Hilbert wrote:
> 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
>

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





PostPosted: 2006-7-16 7:27:00 Top

java-programmer >> why allow multiple classes in one file?
"Chris" wrote:
> Hilbert wrote:
>> 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
>>
>
> 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.

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





PostPosted: 2006-7-16 21:19:00 Top

java-programmer >> why allow multiple classes in one file? Thanks for the info!

Thanks,
Hilbert

Adam Maass wrote:
> "Chris" wrote:
> > Hilbert wrote:
> >> 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
> >>
> >
> > 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.
>
> 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