Importing Package

  • Using fully qualified name

    A class from a package can be used in program by specifying fully qualified name of it. For example app package having a class start.

    Fully qualified name of a class is package name followed by class name.

    Fully qualified name :

    
    class resume extends app.start 
    {
    // class definition
    }

  • Importing required class

    In first approach fully qualified name has to be specified every time there is need to use package's class. In order to avoid this extra work, import statement can be used. import keyword used to import required classes in program so that those classes can be used in same manner as those classes are present in program.

    Syntax :

    
     import <package_name>.<class_name>;
          

    Program given below using import statement to import class start in program.

    Example :

    
    import app.start; 
    class resume extends start
    {
    //definition of class
    }



  • Importing package

    If a package having twenty classes and we need all those classes then one way to import all classes is to import every class separately. it means we need to write 20 import statement one for each class.


    Another solution is to import entire package using single import statement.


    Syntax :

    
     import <package_name>.*;
          

    In our program we want to use class one and class start from package app. So we will import package and then we can use the classes.

    Example :

    
    import app.*; 
    class resume extends start
    {
    //definition of class
    }

  • Note ! A package can also contain any number of packages. if a package is imported in a program then only classes and interface of that package will be imported to program.

    For example package jack having two classes and one package mac. Package mac having three classes. Now if package jack is imported using import java.*; statement then two classes of jack package will be imported only. In order to import classes of mac package import java.mac.* ; statement has to be used.

  • Static import

    Static import is an important feature of import keyword. static import is used to import static member of a class. Static member are used with class name. After importing static member those member can be used without any class name.


    Syntax :

    
    import static <package>.<class>.<static_member>;
    
    

    Above static import statement only import specified static member.
    In order to import all static member of a class following static import is used.

    Syntax :

    
    import static <package>.<class>.*;
    
    


    Example :

    
    class one 
    {
    public static void main(String arg[])
    {
    System.out.println("square root of 16 is "+Math.sqrt(16));
    }
    }
    /*Output:
    square root of 16 is 4
    */

    Above program used a static method of class Math . Static methods are invoked by using class names. Same program can be written using static import . Math class belongs to java.lang package and we didn't import java.lang package then how our program is accesing class Math. Answer of this question is that java compiler automatically import java.lang package in each java code file


    Syntax :

    
    import static java.lang.Math.*;
    class one
    {
    public static void main(String arg[])
    {
    System.out.println("square root of 16 is "+sqrt(16));
    }
    }
    /*Output:
    square root of 16 is 4
    */