Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

10. Numbers and Statics: Numbers Matter > Even more Statics!... static imports

Even more Statics!... static imports

New to Java 5.0... a real mixed blessing. Some people love this idea, some people hate it. Static imports exist only to save you some typing. If you hate to type, you might just like this feature. The downside to static imports is that - if you’re not careful - using them can make your code a lot harder to read.

The basic idea is that whenever you’re using a static class, a static variable, or an enum (more on those later), you can import them, and save yourself some typing.

Use Carefully: static imports can make your code confusing to read

Some old-fashioned code:

import java.lang.Math;
class NoStatic {
  public static void main(String [] args) {
   System.out.println("sqrt " + Math.sqrt(2.0));
   System.out.println("tan " + Math.tan(60));
  }
}

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial