121k views
3 votes
Assume the file, Lab05.java, uses two classes, which are Lab05 and Util. Which of the following statements is true about the use of these classes?

a. Both classes must be placed in the same file.
b. Each class must be placed in a separate file.
c. It is only possible to place the two classes in separate files if you create a package.
d. It is possible to place both classes in separates files or in the same file.

User Jjpcondor
by
7.0k points

1 Answer

1 vote

Final answer:

It is possible to place both Lab05 and Util in separate files or in the same file, depending on whether they are public or package-private. Separate files are required if both are public, but they can reside in the same file if only one is public.

Step-by-step explanation:

In Java, the question regarding whether both classes, Lab05 and Util, must be placed in the same file or can be placed in separate files has a clear answer. The correct statement is d. It is possible to place both classes in separate files or in the same file. However, there are certain rules that apply. If both classes are public, each must be in its own separate file, with the file name matching the class name. If only one class is public, and the other has default or package-private access, they can both reside in the same file, but the file name must match the public class name. It is not necessary to create a package to separate classes into different files, although using packages is a common practice to organize classes in larger projects.

User Rayleone
by
7.2k points