Skip to main content

Static Import Statement

     Static Import Statement


1.5v new features 

  • for-each loop
  • var-args
  • AutoBoxing and AutoUnBoxing
  • Generic Method
  • co-variant return type.
  • Queue
  • Annotation 
  • enum
  • static import
                                                            Static Import
  • introduce in 1.5v 
  • according to sun uses of static import reduces the length of the code and improves readability but according world wide programming expert (like us uses of static import creates confusion and reduces readability hence if there is no specific requirement then it is not recommended to use static import.
  • usually, we can access static members using the class names but whenever writing static import we can access static members directly without a class name.
without static import-:
 
class Test{
public static void main(String[] args){
    System.out.print(Math.sqrt(4));
    System.out.print(Math.max(10,20));
    Sysstem.out.print(Math.random());
}
}


with Static import-:

import static java.lang.Math.*;
class Test{
 public static void main(String[] args){
      System.out.print(sqrt(4));
      System.out.print(max(10,20));
      System.out.print(random());
 }
}

class Test{
    static PrintStream out;
}
System.out.println();

explain about System.out.println()-:
System-: System is the class.
out-: out is the static variable present in the System class of the type PrintStream.
println()-:println() is the method present in the printStream.

out is a static variable present in the system class hence we can access by using the class name System but whenever we are writing static import it is not required a class name and we can access it directly.

import static java.lang.System.out;
class Test{
   public static void main(String[] args){
       out.println("hello");
       out.println("hi");
  }
}

class Test{
    static String s="java";
}
Test.s.length();

where 
Test-:Test is the class.
 s-:s is the static variable present in the Test class of the type of java.lang.String.
length()-: length() is the method present in the String class.


import java.lang.Integer.*;
import java.lang.Byte.*;
class Test{
  public static void main(String[] args){
          System.out.print(MAX_VALUE);
}
}
compile time error-:reference to MAX_VALUE ambiguity problem

while resolving static member compiler will always consider the precedence in the following order
  • current class static members
  • Explicit static import
  • Implicit static import
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Byte.*;
class Test{
   public static void main(String[] args){
        static int MAX_VALUE=999;
        System.out.print(MAX_VALUE);
   }
}

output-: 999




import static java.lang.Integer.MAX_VALUE;
import static java.lang.Byte.*;
class Test{
   public static void main(String[] args){
        //static int MAX_VALUE=999;
        System.out.print(MAX_VALUE);
   }
}
if comment static member then static explicit static import and hence Integer class max_value will be considered in this case the output is .
output-:2147483647

//import static java.lang.Integer.MAX_VALUE;
import static java.lang.Byte.*;
class Test{
   public static void main(String[] args){
        //static int MAX_VALUE=999;
        System.out.print(MAX_VALUE);
   }
}
if we comment both static member and Explicit import then implicite static import will be consider in this case output is.
output-:127


Normal Import Syntax
 Explicit import-:
    import packageName.class;
    eg-:
        import java.util.ArrayList;

Implicit import-:
     import packageName.*;
     eg-:
           import java.util.*;

Static Import Syntax
Explicit import-:
    import static packageName.className.staticMethod/staticVariable;
    eg-:
        import static java.lang.Math.sqrt;
        import static java.lang.System.out;

Implicit import-:
     import static packageName.className.*;
     eg-:
           import static java.lang.Math.*;
           import static java.lang.System.*;

Ques-: which of the following statements are valid?

import java.lang.Math.*;                       //invalid
import static java.lang.Math.*;           //valid
import java.lang.Math.sqrt;              //invalid
import static java.lang.Math.sqrt();//invalid
import java.lang.Math.sqrt.*;        //invalid
import static java.lang.Math.sqrt;//valid
import java.lang;                            //invalid
import static java.lang;                //invalid
import java.lang.*;                        //valid
import static java.lang.*;            //invalid



  • Two packages contain classes or interface with the same name is very rare and hence ambiguity problems very rare in normal import.
  • but two classes and interface contain a variable are method with same name is very common and hence ambiguity problem is also very common problem in static import.
  • uses of static import reduces readbility and creates confusion and hence if there is no specific requirement the its not recommended to use static import.
difference b/w normal import and static import -:
Normal Import-:
  • we can use normal import to import classes and interface of a particular package
  • whenever we are using normal import  it is not required to use fully qualified name we can use short name directly
Static Import-:
  • we can use static import to import static members of a particular class or interface 
  • whenever we are writing static import it is not required to use a class name to access static member and we can access them directly.

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...