Java Package

A Package can be described as collecting related types (classes, interfaces, counts and annotations) giving access security and namespace administration. Packages are utilized as a part of Java to avert naming clashes, control access, make seeking/placing and utilization of classes, interfaces, identifications and annotations less demanding, and several others.

In java, a package can be classified in two forms, i.e. built-in package and user-defined package.

A few packages available in Java are:

  • java.io – all the classes for output and input are available in this package java.
  • java.lang – all the major classes are available in this package

Developers can create their packages or package collections of classes/interfaces, and so on. It is a decent practice to collect related classes executed by you so that a software engineer can undoubtedly discover that the interfaces, classes, annotations and counts can be connected. Since the package makes another namespace, there won’t be any name clashes with different packages. Utilizing packages, it is less demanding to give access control and it is likewise simpler to find the related classes.

When you create a package, you ought to pick a name and explain with that name at the highest point of each source record that contains the classes, interfaces, lists, and annotation sorts that you need to incorporate in the package. The package declaration ought to be the first line in the source record. There can be the only declaration in each one source record, and it applies to various sorts in the file. In the event that a declaration of interface is not utilized, then the interfaces, class, annotation and specifications will be put in a package, which will be unnamed.

The package Keyword

Package hierarchy is specified via the package keyword preceding a class definition as shown below. Here, class Demo belongs within package A. Its complete qualified name is thus A.Demo.

		package A;
		class Demo {
		int h;
		void j() { ... }
		}
	

It follows that a “package B.C” prefix before “class Demo2” definition makes class Demo2 belong to the C package that is in the B package.

		package B.C;
		class Demo2 {
		int y;
		void z() { ... }
		}
	

Thus far, we have been using class definitions but without any package prefix. Such classes belong to the top-level anonymous package The Java virtual machine maps the package hierarchy onto its directory structure for locating and loading code files. As such, code for A.Demo will be at Demo.class in directory A, or more succinctly, the pathname A/Demo.class. Similarly, B.C.Demo2 will be found at B/C/Demo2.class.

The import keyword

The import keyword provides the complement function of the package facility. While the package keyword places succeeding class definitions in a separate namespace of the said package, the import keyword makes package constituents visible.

Continuing from our earlier Demo2 example, any client code outside of package B.C must either refer to it in its qualified form:

		class another {
		B.C.Demo2 x = new B.C.Demo2();
		...
		}
	
		or, import the complete name space of B.C package
	
		import B.C.*;
		class another {
		Demo2 x = new Demo2();
		...
		}
	
		or, import the complete name space of B.C package

		import B.C.*;
		class another {
		Demo2 x = new Demo2();
		...
		}

		import B.C.Demo2;
		class another {
		Demo2 x = new Demo2();
		...
		}

		Since the class RST is now used outside its package, RST must be a public class.
		package B.C;
		public class Demo2 {
		int y;
		void z() { ... }
		}
	
Access Package without import keyword

Suppose we use fully qualified name to import any class into your program. In that case, only that particular class of the package will be accessible in our program, other classes in the same package will not be accessible. For this approach, there is no need to use the import statement. But we will have to use the fully qualified name every time we are accessing the class or the interface. This is generally used when two packages have classes with same names. For example: java.util and java.sql packages contain Date class.

Example
		//save by X.java
		package example;
		public class X{
		  public void msg(){System.out.println("Hello Java ");}  
		}  //save by Y.java  
		package myexample;  
		import example.*;  

		class Y{  
		  public static void main(String args[]){  
		   X obj = new X();  
		   obj.msg();  
		 }  
		}  
	
Output

Hello Java

Import Specific Class

To access only specific class in our program, then import package.classname to declared class of this package.

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

		//save by Test.java  
		package myexample;  
		import example.Demo;  
		class Test {  
		    public static void main(String args[]) {  
		        Demo obj = new Demo();  
		        obj.msg();  
		    }  
		}
	
Output

Hello java class

Import all classes of package
		//save by Example1.java  
		package learnjava;  
		public class Example1{  
		    public void msg() {
		        System.out.println("Hello Example");
		    }  
		}  

		//save by Example2.java  
		package Java;  
		import learnjava.*;    
		class Example2 {  
		    public static void main(String args[]) {  
		        Example1 obj = new Example1();  
		        obj.msg();  
		    }  
		}
	
Output

Hello Example

Copyright 1999- Ducat Creative, All rights reserved.