Friday, October 29, 2010

Static Import

Hi ,

     While learning JUnit 4 , I came across  Static Imports of JAVA quite useful feature. Its syntax looks like

import static org.junit.Assert.*; 

    This allows the importing class to access static public members of Assert package without class name.

fail("Not yet implemented"); 

          The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.


       The static import can also import individual static member of class .

package com.blogspot.pravingole.demo;

import static java.lang.Math.PI;
import static java.lang.Math.*;

public class TestStaticImport {

 public static void main(String[] args) {
  float r = 5.1f;
  System.out.println("Circle area is : "+ (PI*r*r));
 }
}


 for more details click here

P.S. :  This static import don't  become member of importing class, so subclass can't access them without import.

No comments: