Packages in Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Packages In Java

Package in java is a mechanism to encapsulate a group of classes, sub packages and

interfaces. Packages are used for:

 Preventing naming conflicts. For example there can be two classes with
name Employee in two packages, college.staff.cse.Employee and
college.staff.ee.Employee
 Making searching/locating and usage of classes, interfaces, enumerations
and annotations easier
 Providing controlled access: protected and default have package level
access control. A protected member is accessible by classes in the same
package and its subclasses. A default member (without any access
specifier) is accessible by classes in the same package only.
 Packages can be considered as data encapsulation (or data-hiding).

All we need to do is put related classes into packages. After that, we can simply write an
import class from existing packages and use it in our program. A package is a container
of a group of related classes where some of the classes are accessible are exposed
and others are kept for internal purpose.We can reuse existing classes from the
packages as many time as we need it in our program

Creating a package in java:Creating a package in Java is a very easy task.

Choose a name for the package and include a package command as the first statement

in the Java source file. The java source file can contain the classes, interfaces,

enumerations, and annotation types that you want to include in the package. For

example, the following statement creates a package named MyPackage.

Including a Class in Java Package


To create a class inside a package, you should declare the package name as the first

statement of your program. Then include the class as part of the package. But,
remember that, a class can have only one package declaration. Here’s a simple

program to understand the concept.

package MyPackage;

public class Compare {


  int num1, num2;
  Compare(int n, int m) {
     num1 = n;
     num2 = m;
}
public void getmax(){
    if ( num1 > num2 ) {
     System.out.println("Maximum value of two numbers is " + num1);
  }
    else {
     System.out.println("Maximum value of two numbers is " + num2);
    }
}
public static void main(String args[]) {
Compare current[] = new Compare[3];
  current[1] = new Compare(5, 10);
    current[2] = new Compare(123, 120);
    for(int i=1; i < 3 ; i++)
    {
     current[i].getmax();
    }
 }
}
Output:

Maximum value of two numbers is 10


Maximum value of two numbers is 123

Creating a class inside package while importing another package:


 
package svew;
import MyPackage.Compare;
public class Demo{
    public static void main(String args[]) {
        int n=10, m=10;
        Compare current = new Compare(n, m);
        if(n != m) {
             current.getmax();
        }
        else {
            System.out.println("Both the values are
same");
        }  
}
}
Output:

1
Both the values are same
I have first declared the package svew, then imported the class Compare from the

package MyPackage. So, the order when we are creating a class inside a package

while importing another package is, 

  Package Declaration
  Package Import
    
If you do not want to use the import statement, there is another alternative to access a
class file of the package from another package. You can just use a fully qualified name
while importing a class.

Using fully qualified name while importing a class

Here’s an example to understand the concept. I am going to use the same package that

I have declared earlier in the blog, MyPackage.

 
Package svew;
public class Demo{
    public static void main(String args[]) {
        int n=10, m=11;
        //Using fully qualified name instead of import
        MyPackage.Compare current = new MyPackage.Compare(n,
m);
        if(n != m) {
             current.getmax();
        }
        else {
            System.out.println("Both the values are same");
        }  
}
}
Output:

 
Maximum value of two numbers is
11

Static Import in Java


Static import feature was introduced in java from version 5. It facilitates the Java

programmer to access any static member of a class directly without using the fully

qualified name.

 
package MyPackage;
import static java.lang.Math.*; //static import
import static java.lang.System.*;// static import
public class StaticImportDemo {
       public static void main(String args[]) {
          double val = 64.0;
          double sqroot = sqrt(val); // Access sqrt() method
directly
          out.println("Sq. root of " + val + " is " + sqroot);
          //We don't need to use 'System.out
       }
    }
Output:
1
Sq. root of 64.0 is 8.0

You might also like