Wednesday 26 March 2014

Static import

All the static members are accessed by using the ClassName. By using static import we can call the variable or method with name itself, no need to call the members with ClassName as prefix.

Without using static import
import java.lang.Math;
class WithOutStaticImport{
 public static void main(String args[]){
  System.out.println("Square root of 100 is " + Math.sqrt(100));
 }
}

Output
Square root of 100 is 10.0

With static import
import static java.lang.Math.*;
class WithStaticImport{
 public static void main(String args[]){
  System.out.println("Square root of 100 is " + sqrt(100));
 }
}

Output
Square root of 100 is 10.0



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment