C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Packages in Java

Learn: What is Package? Learn how to create a Java Package, with its advantages?
Submitted by Abhishek Jain, on August 29, 2017

In programming, developers tend to group related types into groups so that they can be easier to find and to avoid conflicts in names. These groups are called packages. They can define their own packages to bundle group of classes/interfaces etc.

Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the related classes.

Some of the existing packages in Java are:

  1. java.lang - contains the fundamental classes.
  2. java.io - contains the classesused for reading and writing(input/output).

Steps to create a Java package:

  1. Come up with a package name.
  2. Pick up a base directory.
  3. Make a subdirectory from the base directory that matches your package name.
  4. Place your source files into the package subdirectory.
  5. Use the package statement in each source file.
  6. Compile your source files from the base directory.
  7. Run your program from the base directory.

Creating a Java Package

The first step is (rather obviously) to create the directory for the package. This can go in one of two places:

  • In the same folder as the application that will be using it
  • In a location listed in the system CLASSPATH environmental variable

When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes/interfaces or enumerations that you want to include in the package.

Syntax:

package<package_name>;

The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

If a package statement is not written then the class/interfaces or enumerations will be put into an unnamed package.

Example:

For example, say that we put the file TestA.java into the directory mypack, which is a subdirectory of myApps. So, on a MS Windows platform the file path looks like c:\myApps\mypack\TestA.java.

At the top of each file we put the statement package mypack; as shown in the following code:

myApps/mypack/TestA.java

package mypack;

public class TestA
{
	private int i;
	
	public TestA (int arg1) 
	{
		i = arg1;
	}
	public void show()
	{
		System.out.println(i);
	}
}

We can illustrate how to use a package with TestA.java which we put in the next directory above that of the package mypack. If you working in current folder then there is no need to set the classpath.

But when you are importing packages from other drive or from other folder then it is necessary to set the classpath.

set classpath=c:\myApps\mypack;

Program to import packages:

Import mypack.*;

Class Testpack
{ 
	public static void main(String arg[])
	{ 
		TestA A = new TestA(34);
		A.show();
		
		TestA B = new TestA(45);
		B.show();
	}
}

Output

34
45

Advantages

  • To bundle classes and interfaces.
  • To know where programmers to find types that can provide related type functions.
  • The Classes of one Package are isolated from the classes of another Package.
  • The names of your types won't conflict with the type names in other Packages because the Package creates a new Namespace.
  • Packages provide reusability of code.
  • We can create our own Package or extend already available Package.








COMMENTS