Hi experts,

I have confusion about the point I read in my book:
I. In a source file you can define number of classes, but only one of them can be a public class. In this case, the name of source file must match the name of public class.
II. In a source file, you can define number of classes with default access specifier.

As I understand, one class must be public that is startup class and one or more class can be package(default).

1. Suppose I have coded two classes in a source file, say in package X. And I want to use both of them in package Y. as far as I know, to use in outside package we have to put classes in access specifier public!

Should I have to put both the classes in different source file or what?

Thanks in advance for your cooperation all the way…


yeah if you want to use that class outside the package then you need to create separate class with public access modifier. Know you will be thinking that what is the use of defining multiple classes in one src file, reason is :
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.

This question has already been answered. Start a new discussion instead.