Java package

PACKAGE

Package is a container which is collection of classes, interfaces and sub packages, subpackage is collection of classes , interfaces and sub packages. The main advantage with package is common reusability that means the predefined properties of classes and interfaces of package can be used in any java program without rewriting of code. In java language packages are classified into two types.
  • Predefined packages.
  • User defined packages.
  • Predefined packages:

    These are the packages already designed by sun micro system and supplied as a part of java API and these properties commonly used by every java developer in the world. As per as core java is consent following are mostly used predefined packages.

    Java.lang package:

    Which is collection of predefined classes and interfaces used to handle various operations like multi threading , exception handling ,converting string values into primitive data types, string handling etc…
    Syntax:
    Import java .lang.class name/interface name;
    Used to import a specific property of a package.
    Or
    import java.lang.*;
    In the above syntax import is a keyword used to import the predefined properties of java API into current java program. Java is a main package and lang is a sub package.

    NOTE: In java language every package will be treated a separate directory of folder and sub package represents sub directory.




    Java.awt package:

    Which is a collection of predefined classes and interfaces used to design a GUI applications (graphical user interface).
    Syntax:
    import java.awt.*;

    Java.awt.event:

    Which is collection of predefined classes and interfaces used to handle the event(performing action)in GUI application.
    Example: If a button clicked sending the request to other location.
    import java.awt.event.*;

    NOTE: Here * represents only classes and interfaces but not packages.

    Java.io package:

    Which is collection of predefined classes and interfaces used to perform various operations on file like writing into a file reading data from the file etc…
    Import java.io.*;

    Java.net package:

    Which is collection of predefined classes and interfaces used to develop basic networking based applications like LAN based application and client server applications. import java.net.*;

    Java.applet package:

    Which contains only one predefined class used to develop distributed applications at core java level. import java.applet.Applet; Or Import java.applet.*;

    Java.util package:

    Which is collection of predefined classes and interfaces used to perform various operations like Handling of data structure. Handling of date and calendar classes. Etc.. Import java.util.*;

    Java.text package:

    Which is collection of predefined classes and interfaces used to perform various conversions like data conversion ,currency conversation etc …
    import java.text.*;

    Note: The predefined packages available in the form of rt.jar file in the jdk software
    C:programfiles/java/jdk1.6/jre/lib.

    Click here to show all predefined package

    User defined packages:

    If any package is designed by the user and if it is used by one or more specific java developers known as user defined package.
    While developing real time application it is highly recommended to keep all the programs in a package structure.
    In java language user defined packages can be created with the help of package keyword.
    Syntax:
    Package pack1.pack2. ……;
    In the above syntax pack1 is the main package,pack2,…..are the sub packages.

    Rules to design a package:

    1. Create a package with valid user defined name
    2. Note: Package statement should exist as first statement of package program.

    3. Package program can contains either only one public class or only one public interface(package program can contain any number of normal classes and interfaces.
    4. Create a public method in a given class
    5. Package program should not contain any main method.
    6. Every package program should be saved with either public class name.java of public interface.java
    7. Compile the package program with the special syntax is
    8. javac -d . programname.java

    Syntax:
    package pack1.pack2…….;
    public class <claaname>
    {
    ------
    ------
    }
    class  <classname>
    {
    ------
    ------
    -
    -
    

    Note: Package program can be compile con’t be executed because of no main method.

    Package Program :

    package sateesh; public class Rain { public void myRain() { System.out.println("Welcome Rain"); } }

    Compile:
    javac -d . Rain.java

    Note:Do not run the package Program

    Call the Package Program

    import sateesh.Rain;
    class Demo
    {
    public static void main(String args[])
    {
    Rain v=new Rain();
    v.myRain();
    }
    }
    

    Compile:
    javac Demo.java

    Run:

    Java Demo

    Welcome Rain