Java Packages

Published by Admin on

Package- Most Powerful Feature In Java

The concept of packages is similar to the concept of header files in C and C++. In Java Package is collection of grouping of similar type of Classes, sub-classes, interfaces, enumerations, annotations, and sub-packages based on there functionality. When application is written using Java Programming language, it can be create hundreds, thousands or more than that of individual classes. Packages makes to keep things organized by placing related classes and interfaces into related packages. Packages names are always written in small or lower case.

Advantages of Java Packages

Packages have several advantages like code re-usability, unique identity of a class, data hiding , access protection. It is easy to maintain. It is always good practice to create and use packages while written Java Application.

There are two types of Packages in Java:-

  1. Built-In Packages (Java API)
  2. User Defined Packages (your own created packages)

1. Build-In Packages

JDK provides pre-written classes, sub-classes, interfaces and so on. It is simplify Java coder, to use features provided by Java API. For ex. java.lang, java.io, java.util, java.applet, etc. Java.lang package is the only package does not need to be imported in java program, by default it is there.

The import statement (import is a reserved word) is used to import packages.

Syntax of import statement:-

import packagename.className;  //for single class import from package
import packagename.interfaceName;  //for single interface import from package
import packagename.*; //for all classes import from package

import java.io.*; or
import java.io.DataInputStream;

Here is the simple example of Build-In Packages.

import java.util.Scanner;
class TestBuildInClass{
     public static void main(String args[]){
          Scanner getObj=new Scanner(System.in);   
          System.out.println("Enter Email ");
          String email= getObj.nextLine();
          System.out.println("Email Id is: " + email);
     }
}

Static Import Statement

JDK1.5 & higer versions provides concept of static imports. The syntax of a static import is as follows:

import static packageName.ClassName.*;   //OR
import static packageName.ClassName.MemberName;
//For Example
import static java.lang.System.*;

If the above statements is add to the top of source file then user can use all static variables & static members of System class without classname prefix.

import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;
class Statictest{
          Scanner sc=new Scanner(in);  
/*  Here in is static member of System class, we can directly use in member without System class prefix. Because we import static System class*/
         double x,y,z;
      Statictest(){
         out.println("Enter two double number");
         x=sc.nextDouble();
         y=sc.nextDouble();
         z=pow(x,y);
         out.println(x+" power "+y+"="+z);
      }   
      public static void main(String args[]){
            new Statictest();
      }
}

2. User Defined Packages

User Defined Packages are those you create your own packages. With the help of example, we create packages and write java program and execute them.

For creating packages in java, we need to create folder in file system directory(Any drive folder for ex. E:) to store them

For Example In E: we created package name as packageP1 uner that we created class Packagedemo.java
  └── packageP1
    └── Packagedemo.java

For create a package, use the keyword package and class Packagedemo.java

package packageP1;
class Packagedemo{
  public static void main(String[] args) {
    System.out.println("This is demo package.");
  }
}

Save the file as Packgedemo.java, and compile it:

E:\Users\PCAdmin>javac Packagedemo.java

then compile package

E:\Users\PCAdmin>javac -d . Packagedemo.java

When we compile the example, packageP1 name folder was create. In that our class Packgedemo.java is create.

To run classfile E:\Users\PCAdmin>java packageP1.Packagedemo


Question :-

  • Which package is always imported by default?
  • What is a Java package and how is it used?
  • Explain the usage of Java packages?


0 Comments

Leave a Reply

LinkedIn
LinkedIn
Share
Instagram
Follow by Email
YouTube
YouTube
Telegram
RSS