Wednesday, November 22, 2017

import static in Java

example.
import java.lang.Math;

class WithoutStaticImports {

 public static void main(String [] args) {
  System.out.println("round " + Math.round(1032.897));
  System.out.println("min " + Math.min(60,102));
 }
}
Same code, with static imports:
import static java.lang.System.out;
import static java.lang.Math.*;

class WithStaticImports {
  public static void main(String [] args) {
    out.println("round " + round(1032.897));
    out.println("min " + min(60,102));
  }
}
Note: static import can make your code confusing to read.

What does the “static” modifier after “import” mean?

No comments:

Post a Comment