Demonstrating static Import

Figure 8.14 demonstrates a static import. Line 3 is a static import declaration, which imports all static fields and methods of class Math from package java.lang. Lines 9–12 access the Math class’s static fields E (line 11) and PI (line 12) and the static methods sqrt (line 9) and ceil (line 10) without preceding the field names or method names with class name Math and a dot.

 1   // Fig. 8.14: StaticImportTest.java 2   // Static import of Math class methods. 3   import static java.lang.Math.*; 4  5   public class StaticImportTest 6   { 7      public static void main(String[] args) 8      { 9         System.out.printf("sqrt(900.0) = %.1f%n", sqrt(900.0));10         System.out.printf( ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.