Hence, there's a convention to create a package as a domain name, but in reverse order. The package name must be unique (like a domain name). Here, the Test class now belongs to the com.test package. Here, any class that is declared within the test directory belongs to the com.test package. Now, edit Test.java file, and at the beginning of the file, write the package statement as: Let's create a Java file inside another directory. Java uses file system directories to store packages. To define a package in Java, you use the keyword package. These packages are called user-defined packages. Java also allows you to create packages as per your need. To use it, we have to import the package first using the import statement. The ArrayList class belongs to java.util package. For example, java.lang, java.util, java.io, etc.
- this is a SQL Date used for the SQL query and such.īased on whether the package is defined by the user or not, packages are divided into two categories: Built-in Packageīuilt-in packages are existing java packages that come along with the JDK.- this is a normal Date class that can be used anywhere.This was possible because these two Date classes belong to two different packages: How did they manage to include two classes with the same name Date in JDK?
However, the rule of thumb in Java programming is that only one unique class name is allowed in a Java project. These packages help you to reserve the class namespace and create a maintainable code.įor example, you can find two Date classes in Java. If you want to use the ResultSet interface in your code, just import the java.sql package (Importing packages will be discussed later in the article).Īs mentioned earlier, packages are just containers for Java classes, interfaces and so on. The package contains all the related types that are needed for the SQL query and database connection. For example, in core Java, the ResultSet interface belongs to the java.sql package. A package is simply a container that groups related types (Java classes, interfaces, enumerations, and annotations).