Home >>Java Tutorial >Package in Java

Package in Java

Package in Java

Java Packages are generally used in order to control access, occluding the naming conflicts and many more. A Package in Java can also be defined as a grouping of the related types (classes, annotations interfaces, and enumerations) that are delivering access protection and namespace management.

There are many types of packages in the Java language and here are the two of them depicted below:

  • java.lang – These are basically used in order to bundle the fundamental classes
  • java.io − classes for input , output functions are generally bundled in this package of Java language

Users of the language can define their own packages in order to bundle group of classes/interfaces, etc. This is considered as a good practice that is being used to group the related classes that has been implemented by the user so that a user can easily determine that the classes, interfaces, enumerations, and annotations are related to this.

There won't be any name conflicts with names in other packages as the package has created a new namespace. Using these packages, it has become extremely easier to deliver the access control and locating the related classes have become much easier.

In layman's terms the Java package can be understood as the group of similar types of classes, interfaces and sub-packages. Package in the java language can be categorized generally in two form, built-in package and user-defined package. There are basically many built-in packages that are available in Java language like, lang, awt, javax, swing, net, io, util, sql etc.

Creating a Package

At the time of creating a package, the programmer should be able to choose a name for the package and must include a package statement along with that name at the very top of every source file that consists of the classes, interfaces, enumerations, and annotation types that the programmer wants to include in the package.

The package statement in the Java language must be the first line in the source file. Please note that there can be only one package statement in each of the source file and this thing applies to all the types in the file. In case, a package statement is not used then the class, interfaces, enumerations, and annotation types will be taken inside the current default package that is present in the language. In order to compile the Java programs with the package statements, the programmers have to use -d option that has been depicted below:

javac -d Destination_folder file_name.java

After this point, a folder with the given package name will be created in the specified destination, and the compiled class files will be taken to that folder.

Advantage of Java Package

There are many advantages of the Java Package that have been proven to be extremely useful in terms of the web development and here are some of the most important advantages of the packages have been shown below:

  • In order to categorize the classes and interfaces so that they can be easily maintained, the Java package is used.
  • Java package are known to deliver access protection.
  • Java package basically removes the naming collision.

Simple example of java package

package mypackage;  
public class SimplePackage
{  
 public static void main(String args[])
 {  
    System.out.println("Welcome to package");  
   }  
}

The import Keyword

In case a class in the Java language wants to use another class that exists in the same package, then the package name is not necessary to be used. Classes that are in the same package find each other without any special syntax.

Here is an example that will be clearing all your doubts. In this example, a class that has a name Boss is added to the payroll package that already includes Employee. The Boss class can then refer to the Employee class without even using the payroll prefix. Look at the example for the application process of this concept:

Example

package payroll;
public class Boss 
{
   public void payEmployee(Employee e) 
   {
      e.mailCheck();
   }
}

The Directory Structure of Packages

There are mainly two major results that occur when a class is placed in a package that are as depicted below:

  • The name of the package basically becomes a part of the name of the class its basically the same as the concept that has been discussed previously.
  • The name of the package should match the directory structure where the corresponding bytecode is placed and it is mandatory.

How to access package from another package?

There are generally three ways by which the user can access the package from outside the package that are as depicted below:

  • import package.*;
  • import package.classname;
  • fully qualified name.

Let's discuss them one by one:

1. Using packagename.*

If the programmer generally use package.* then all the classes and interfaces that belongs to this package will be accessible except the subpackages. The import keyword is generally used in order to make the classes and interface of another package very accessible to the current package.

Here is an example

//save  Demo.java  
package pack;  
public class Demo
{  
  public void msg(){System.out.println("Welcome");}  
}  
//save by Demo1.java  
package mypack;  
import pack.*;  
  
class Demo1
{  
  public static void main(String args[])
  {  
   Demo obj = new Demo();  
   obj.msg();  
  }  
}  
Output :Welcome

2. Using packagename.classname

If the programmer imports the package.classname then only the declared class of this package will be accessible.

Here is an example

//save Demo.java  
package pack;  
public class Demo
{  
  public void msg()
  {
  System.out.println("Welcome");
  }  
}  
//save this Demo1.java  
package mypack;  
import pack.Demo;  
  
class Demo1{  
  public static void main(String args[])
  {  
   Demo obj = new Demo();  
   obj.msg();  
  }  
} 
Output :Welcome

3. Using fully qualified name

If the programmer wants to use fully qualified name only then the declared class of this package will be accessible. In this case there is no need to import it. Instead the programmer needs to use the fully qualified name every single time when they will be accessing the class or interface. Please note that it is generally used when the two packages possess the same class name for instance, java.util and java.sql packages contain Date class.

Her is an example

//save as Demo.java  
package pack;  
public class Demo
{  
  public void msg()
  {
  System.out.println("Welcome");
  }  
}	
//save as Demo1.java  
package mypack;  
class Demo
{  
  public static void main(String args[])
  {  
   pack.Demo obj = new pack.Demo();  
   obj.msg();  
  }  
}  
Output :Welcome