Package in Java

Packages is a way of grouping related classes, interfaces and enums. Using package, you can avoid clashing of names and control accessibility of classes and interfaces.

There are two types of packages in Java-

  1. Built-In Package- Oracle builds these packages. Example- java.ulil, java.io, java.net, java.lang, etc.
  2. User-Defined Package- These packages are created by users.

How to create package in Java

With the help of package statement, you can create a package. This statement consists of package keyword followed by the package name.

Package statement must be the first statement in a source code. Organisation must use reverse domain name to name their package. For example, if your domain name is tutorialsandyou.com, then package name will be com.tutorialsandyou.

The package represents folder on a file system. When you compile a program having a package statement in it, then the compiler creates a folder with the same name as that package name and places the generated .class file into that folder.

Example of Java package

//Save as Square.java
package mypackage1;
public class Square
{
	public void area(int side)
	{
		System.out.println("Area of square = "+side*side);
	}
	public static void main(String args[])
	{
		Square s = new Square();
		s.area(5);
	}
}

How to compile package program in Java

Follow the below syntax to compile your program

javac -d DirectoryName filename.java

For example

javac -d . Square.java

-d option is used to specify the destination folder where the generated .class file will be placed.

How to run package program in Java

Use the fully qualified name of .class file to run the program.

java mypackage1.Square

Output

Area of square = 25

import statement in Java

The import statement is used to access classes and interface present in different package without using their fully qualified name. There are three different ways to access package from another package-

  1. Importing single member of a package
  2. Importing all the members of a package
  3. Using fully qualified name

Note- The sequence of writing the program is package statement followed by one or more import statement then the class or interface declaration. If you change this sequence, you will get compile time error.

Importing single member of a package

When you want to access a single class from another package, use import packagename.classname

Example

//Save as Square.java
package mypackage1;
public class Square
{
	public void area(int side)
	{
		System.out.println("Area of square = "+side*side);
	}	
}
//Save as Test.java
package mypackage2;
import mypackage1.Square;
class Test
{	
	public static void main(String args[])
	{
		Square s = new Square();
		s.area(5);
	}
}

Output

Area of square = 25

Importing all the members of a package

When you want to access all the classes and interfaces present in another package, use import packagename.*

Example

//Save as Square.java
package mypackage1;
public class Square
{
	public void area(int side)
	{
		System.out.println("Area of square = "+side*side);
	}	
}
//Save as Test.java
package mypackage2;
import mypackage1.*;
class Test
{	
	public static void main(String args[])
	{
		Square s = new Square();
		s.area(5);
	}
}

Output

Area of square = 25

Using fully qualified name

Without using import statement, you can still access classes and interfaces stored in a separate package by using their fully qualified name. But this is not considered a good practice, and you should avoid using it.

Example

//Save as Square.java
package mypackage1;
public class Square
{
	public void area(int side)
	{
		System.out.println("Area of square = "+side*side);
	}	
}
//Save as Test.java
package mypackage2;
class Test
{	
	public static void main(String args[])
	{
		mypackage1.Square s = new mypackage1.Square();
		s.area(5);
	}
}

Output

Area of square = 25

How to compile and run import program in Java

To enable compiler and JRE, to find classes and interfaces of different package add classpath to javac and java command. There are two ways of adding classpath-

  1. Run this command before running javac and java command. set classpath = directorypath;
  2. Add -classpath switch to javac and java command.
    javac -classpath directorypath filename.java
    java -classpath directorypath filename

Static import in Java

Using import static statement, you can import static members of a class or an interface. By using this statement, you can simply access static variables or methods by their name and there no need to prefix them with the class name.

Example of static import

import static java.lang.Math.PI;    
class Test
{
	public static void main(String args[])
	{
		System.out.println("The value of PI is "+PI);
	}
}

Output

The value of PI is 3.1415926