why allow multiple classes in one file?

H

Hilbert

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
 
C

Chris

Hilbert said:
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.
 
A

Adam Maass

Chris" said:
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
 
H

Hilbert

Thanks for the info!

Thanks,
Hilbert

Adam said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top