Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between Import and Static Import in Java.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.56k
    Comment on it

    Import

    • To access classes of one package in another package i.e use external classes without package qualification import feature is used.

    • The classes and interfaces of one package are accessible in another package using import feature.

    Example of import:

    import java.util.Vector;
     
    public class ImportDemo
    {
       public ImportDemo()
       {
    //Imported using keyword, hence able to access directly in the code without package qualification.
         Vector v = new Vector();
     
          // Package not imported, hence referring to it using the complete package.
          java.util.ArrayList list = new java.util.ArrayList();
       }
     
       public static void main(String arg[])
       {
          new ImportDemo();
       }
    }

     

    Static Import

    • The static members of classes defined in one package can be used in another package without class qualification then static import feature is to be used.
    • Static members of class are accessed.
    • Advantage of Static import is it reduces code size but readability of program becomes difficult.
    • Disadvantage of static import is,static members are used without class qualification it appears as it it is declared within called class therefore programmer cannot declare and define another variable of same name. 

    Example of static import:

    import static java.lang.System.*;  //Using Static Import
     
    class StaticImportDemo
    {
       public static void main(String args[])
       {
          //System.out is not used as it is imported using the keyword static
     
          out.println(Hello World);
       }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: