In Section 6.3, you learned about the static fields and methods of class Math. We invoked class Math's static fields and methods by preceding each with the class name Math and a dot (.). A static import declaration (a new feature of J2SE 5.0) enables programmers to refer to imported static members as if they were declared in the class that uses themthe class name and a dot (.) are not required to use an imported static member.
A static import declaration has two formsone that imports a particular static member (which is known as single static import) and one that imports all static members of a class (which is known as static import on demand). The following syntax imports a particular static member:
import static packageName.ClassName.staticMemberName;
where packageName is the package of the class (e.g., java.lang), ClassName is the name of the class (e.g., Math) and staticMemberName is the name of the static field or method (e.g., PI or abs). The following syntax imports all static members of a class:
import static packageName.ClassName.*;
where packageName is the package of the class (e.g., java.lang) and ClassName is the name of the class (e.g., Math). The asterisk (*) indicates that all static members of the specified class should be available for use in the class(es) declared in the file. Note that static import declarations import only static class members. Regular import statements should be used to specify the classes used in a program.
Figure 8.14 demonstrates a static import. Line 3 is a static import declaration, that imports all static fields and methods of class Math from package java.lang. Lines 912 access the Math class's static field E (line 11) and the static methods sqrt (line 9), ceil (line 10), log (line 11) and cos (line 12) without preceding the field name or method names with class name Math and a dot.
Figure 8.14. Static import Math methods.
1 // Fig. 8.14: StaticImportTest.java 2 // Using static import to import static methods of class Math. 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 ", sqrt( 900.0 ) ); 10 System.out.printf( "ceil( -9.8 ) = %.1f ", ceil( -9.8 ) ); 11 System.out.printf( "log( E ) = %.1f ", log( E ) ); 12 System.out.printf( "cos( 0.0 ) = %.1f ", cos( 0.0 ) ); 13 } // end main 14 } // end class StaticImportTest
|
Common Programming Error 8.9
A compilation error occurs if a program attempts to import static methods that have the same signature or static fields that have the same name from two or more classes. |
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover