Java

How to create packages ,import and use them as your own methods in Java4 min read

A package is a group of related classes. A package is used to restrict access to a class and to create namespaces. If all reasonable class names are already used, then we can create a new package (new namespace) and reuse the class names again. For example a package mypackage1 can contain a class named MyClass and another package mypackage2 can also contain a class named MyClass.

Defining or Creating a Package

A package can be created by including the package statement as first line in a Java source file. Syntax for creating or defining a package is given below:




In the above syntax, package is a keyword and mypackage is the name of our package. All the classes that follow the package statement are considered to be a part of the package mypackage.

Multiple source files can include the package statement with the same package name. Packages are maintained as regular directories (folders) in the machine. For example, we have to create a folder named mypackage and store the .class files in that folder.

Following example demonstrates creating a package:

ClassA.java

ClassB.java

In the above example, both ClassA and ClassB belong to the same package mypackage. Remember that the package creation statement should be the first line in the source file.

We can also create a hierarchy of packages as shown below:

Remember that packages are normal folders in the file system. So, pack3 is a sub folder in pack2 and pack2 is a sub folder in pack1.

Java Packages and CLASSPATH

It is not mandatory that the driver program (main program) and the package(s) to be at the same location. So how does JVM know the path to the package(s)? There are three options:

  1. Placing the package in the current working directory.
  2. Specifying the package(s) path using CLASSPATH environment variable.
  3. Using the -classpath or -cp options with javac and java commands.

If no package is specified, by default all the classes are placed in a default package. That is why no errors are shown even if we don’t use a packagestatement.

By default Java compiler and JVM searches the current working directory (option 1) for specified package(s) like mypackage. Let’s assume that our package mypackage is stored at following location:

Then we can set the CLASSPATH (option 2) environment variable (in command prompt) to the location of the package as shown below:

The dot (.) before the path specifies the current working directory and multiple paths can be separated using semi-colon (;).

We can also use -classpath or -cp options (option 3) with javac and java commands as shown below:

In the above example, Driver.java is our main program which utilizes the classes in the package mypackage.

Importing or Using Packages

There are multiple ways in which we can import or use a package. They are as follows:

  1. Importing all the classes in a package.
  2. Importing only necessary classes in a package.
  3. Specifying the fully qualified name of the class.

First way is to import all the classes in a package using the import statement. The import statement is placed after the package statement if any and before all class declarations. We can import all the classes in a package as shown below:

In the above line denotes all classes within the package mypackage. Now you are free to directly use all the classes within that package. A program which demonstrates importing all classes in a package is given below:

If you want to use only one or two classes in a package, the second way is to specify the class names instead of as shown below:

A program which demonstrates importing specific class from a package is given below:

Suppose if two packages contain a class with same name, then it will lead to compile-time errors. To avoid this, we have to use the fully qualified name of the class. A fully qualified name of the class refers to the name of the class preceded by the package name. An example that demonstrates a fully qualified name is given below:

If you want articles like this please take your time to comment us for more.

Leave a Comment