How To Create Package in Java

Package is a collection of predefine classes and interface. Java provides different type of inbuilt packages for different task. Programmer can also create own packages. All Java predefine classes are distributed into several different package.
 
Some package are
  1. lang Package: Lang stands for language. This package contains those class which are essential for every java program.

    E.g.  String and System class

    Lang is the default package of java which means it is optional to import Lang package in program

    Syntax of Import package:-

    import [package name].*; // (all class)
    import [package name].[class name];//(Single class)

  2. io package: io stands for input/output. This package contains those class which are essential for input and output. Some classes of input/output package are;

    E.g. – DataInputStream, BufferReader, DataInoutStramReader

  3. util package: util stands for utility package this package contain different type of classes related to various task.

    E.g.: Date, time, calendar, array, vector. Array List etc.

  4. awt package (swing): awt stands for abstract windowing tool. With the help of this package we can create GUI interface. This package provide many GUI tools.

    E.g.: Command button, choice, list, radio, etc.
    awt contain a sub package name “even”.

  5. applet: with the help of applet function we can create a java program which can be embedded into HTML page by which web browser execute the java program. Web browser must be java enabled.

  6. Sql Package: sql stands for structure query language. This package provides a complete range of classes which is needed for jdbc (java database connectivity). Some important classes of sql package are:

    E.g.: connection, ResultSet, PrepareStatement, etc.

  7. Net package: net stands for networking. This package contains classes to implement or create connection between two or more systems or if we want to implement client server approach then. We can use the classes of net package.

    E.g.: TCP/IP, Request, etc.

How to create package in Java

  1. First create a directory within name of package.
  2. Create a java file in newly created directory.
  3. In this java file you must specify the package name with the help of package keyword.
  4. Save this file with same name of public class
    Note: only one class in a program can declare as public.
  5. Now you can use this package in your program.


Package Pack1
  1. public class Demo  
  2. {  
  3.     public void Show()  
  4.     {  
  5.       System.out.print(“Package called”);  
  6.    }  
  7. }  
After that use this package in your program,
  1. import Pack1.*    
  2. class A    
  3. {    
  4.     public static void main(String …args)    
  5. {    
  6.     Demo ob1= new demo();    
  7. ob1.Show();    
  8. }    
  9. }
Read more articles on Java