Package in java

Package are used in Java, in-order to avoid name conflicts and to control access of class, interface and enumeration etc. A package can be defined as a group of similar types of classes, interface, enumeration and sub-package. Using package it becomes easier to locate the related classes.

There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning of creating and using user-defined packages.

Advantage of Java Package

  • Java package is used to categorize the classes and interfaces so that they can be easily maintained.
  • Java package provides access protection.
  • Java package removes naming collision.
package

Example of java package

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

 	} 

 	} 

How to compile java package

If you are not using any IDE, you need to follow the syntax given below: javac -d directory javafilename

For example javac -d . Simple.java The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).

How to run java package program

To Compile:javac -d . Simple.java

To Run: java mypack.Simple

Output:Welcome to package

How to access package from another package?

There are three ways to access the package from outside the package.

1.import package.*;

2.import package.classname;

3.fully qualified name.

1 Using package name.*

If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. The import keyword is used to make the classes and interface of another package accessible to the current package.

Example of package that import the package name.*

 	//save by A.java 
 package pack; 
 public class A{ 
 public voidmsg(){System.out.println("Hello");} 
 	} 


 package mypack; 
 import pack.*; 

 class B{ 

 public static void main(String args[]){ 
 A obj = new A(); 
 obj.msg(); 
 } 
 } 
  Output:Hello

2 Using packagename.classname

If you import package.classname then only declared class of this package will be accessible.

Example of package by import package.classname


	//save by A.java 
 package pack; 
 public class A{ 
 public void msg(){System.out.println("Hello");} 
 } 

 package mypack; 
 import pack.A; 

 class B{ 
 public static void main(String args[]){ 
 A obj = new A(); 
 obj.msg(); 
 } 
 } 
  	 Output:Hello
Note: If you import a package, subpackages will not be imported.

If you import a package, all the classes and interface of that package will be imported excluding the classes and interfaces of the subpackages. Hence, you need to import the subpackage as well.

Note- The standard of defining package is domain.company.package e.g. com.javatportal.bean orcom.javatportal.dao.

Example of Sub package

Package inside the package is called the sub package. It should be created to categorize the package further

Note- The standard of defining package is domain.company.package e.g. com.javatportal.bean or com.javatportal.dao.

	package com.javatportal.core; 
 class Simple{ 
 public static void main(String args[]){ 
 System.out.println("Hello subpackage"); 
 } 
 } 
To Compile:javac -d . Simple.java To Run: java com.javatportal.core.Simple
Output:Hellosubpackage

How to send the class file to another directory or drive?

There is a scenario, I want to put the class file of A.java source file in classes folder of c: drive. For example:

package1

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

To Compile:e:\source>javac -d c:\classes Simple.java

To Run

To run this program from e:\source directory, you need to set classpath of the directory where the class file resides.

e:\source> set classpath=c:\classes;.;

e:\source> java mypack.Simple

output

Ways to load the class files or jar files

There are two ways to load the class files temporary and permanent.

Temporary

  • By setting the class path in the command prompt
  • By -classpath switch

Permanent

  • By setting the class path in the environment variables
  • By creating the jar file, that contains all the class files, and copying the jar file in the jre/lib/ext folder.

Rule: There can be only one public class in a java source file and it must be saved by the public class name.


	class A{} 
 class B{} 
 public class C{} 

//save as C.java otherwise Compilte Time Error

What about package class?

Package class

The package class provides methods to get information about the specification and implementation of a package. It provides methods such as getName(), getImplementationTitle(), getImplementationVendor(), getImplementationVersion() etc.

Example of Package class

In this example, we are printing the details of java.lang package by invoking the methods of package class.

							

	class PackageInfo{ 
 public static void main(String args[]){ 

 Package p=Package.getPackage("java.lang"); 
 System.out.println("package name: "+p.getName()); 
 System.out.println("Specification Title: "+p.getSpecificationTitle()); 
 System.out.println("Specification Vendor: "+p.getSpecificationVendor()); 
 System.out.println("Specification Version: "+p.getSpecificationVersion()); 
 System.out.println("Implementaion Title: "+p.getImplementationTitle()); 
 System.out.println("Implementation Vendor: "+p.getImplementationVendor()); 
 System.out.println("Implementation Version: "+p.getImplementationVersion()); 
 System.out.println("Is sealed: "+p.isSealed()); 
 } 
 } 


Output:package name: java.lang
 Specification Title: Java Plateform API Specification
 Specification Vendor: Sun Microsystems, Inc.
 Specification Version: 1.6
 Implemenation Title: Java Runtime Environment
 Implemenation Vendor: Sun Microsystems, Inc.
 Implemenation Version: 1.6.0_30
 IS sealed: false

       

Package interview question in core java

  • What is a Java package and how is it used?
  • What is the use of import statement in Java?