July 05, 2012

How it's work "static import" concept in java?


Import means in java; importing the header files from the directory  in java , if put " * " at the end , it loaded all files from directory.

Java compiler execute that class there itself then return the result. but static import same like , this is introduced in java5.0. Directly calling methods and variables using static import with classname.

ex: Collections.synchronizedList(things);


Static imports allow the static items of another class to be referenced without qualification. Used indiscriminately, this will likely make code more difficult to understand, not easier to understand.

Example

import java.util.*;
import static java.util.Collections.*;

public final class StaticImporter {

  public static void main(String... aArgs){
    List things = new ArrayList();
    things.add("blah");

    //This looks like a simple call of a method belonging to this class :
    List syncThings = synchronizedList(things);

    //However, it actually resolves to :
    //List syncThings = Collections.synchronizedList(things);
  }
}


An example in which a static import is likely acceptable is a constants class. For example, a scientific or engineering application might make wide use of Math.PI. A static import of java.lang.Math.* would allow a class to replace Math.PI with PI.

More generally, a business application might define a constants class, and import it statically. This would allow Consts.NEW_LINE to be referenced as NEW_LINE, for example.

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts