Packages
Why package exists and why import is not copying code.
A package is both a namespace and a directory. The line package app; says: this class belongs to the package app, and the file sits in the directory app. The full name of the class becomes app.Main. Without a package, the class goes into the unnamed package. That is fine for a practice file. For a service, the unnamed package is a dead end: other code cannot import it properly.
import java.util.List does not paste the source of List into your file. It lets you write the short name List instead of java.util.List. java.lang is imported on its own: String, Integer and System are available without an import. Importing the whole java.io tree or the network is closed in the training sandbox: those are files and sockets, and the task checks the language, not the machine's environment.
Two classes with the same short name get along fine if their packages differ: shop.Order and billing.Order. The conflict starts when both are imported by their short name. Then you write the full name. This is exactly why packages match directories: otherwise the compiler would not know which file to open.
A public class is visible to other packages. A class without public is visible only to its neighbours in the same package. In a small service this is how internal types are cut off: the package api faces outward, the package internal stays inside, and another module should not import it. A middle developer looks at the package before the method name.
Practice
What import does
What happens when you write import java.util.List?
- The source of List is copied into your file
- You can write List instead of the full name
- List becomes part of java.lang
- The JVM downloads the library from the network
What is already imported
Which package is available without an import line? Write its name.
What is true about a package
Select everything that is true.
- package matches the directory
- import copies the source
- two Order classes in different packages are different classes
- the unnamed package is easy to import