14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Static import 278<br />

Static import<br />

Static import is a feature introduced in the Java programming language that allows members (fields and methods)<br />

defined in a class as public static to be used in Java code without specifying the class in which the field is defined.<br />

This feature was introduced into the language in version 5.0.<br />

The feature provides a typesafe mechanism to include constants into code without having to reference the class that<br />

originally defined the field. It also helps to deprecate the practice of creating a constant interface: an interface that<br />

only defines constants then writing a class implementing that interface, which is considered an inappropriate use of<br />

interfaces [1] .<br />

The mechanism can be used to reference individual members of a class:<br />

import static java.lang.Math.PI;<br />

import static java.lang.Math.pow;<br />

or all the static members of a class:<br />

import static java.lang.Math.*;<br />

For example, this class:<br />

public class HelloWorld {<br />

public static void main(String[] args) {<br />

cm, it has:");<br />

"cm");<br />

System.out.println("Hello World!");<br />

System.out.println("Considering a circle with a diameter of 5<br />

System.out.println("A circumference of " + (Math.PI * 5) +<br />

System.out.println("And an area of " + (Math.PI *<br />

Math.pow(5,2)) + "sq. cm");<br />

}<br />

}<br />

Can be shortened to:<br />

import static java.lang.Math.*;<br />

import static java.lang.System.out;<br />

public class HelloWorld {<br />

has:");<br />

}<br />

public static void main(String[] args) {<br />

}<br />

out.println("Hello World!");<br />

out.println("Considering a circle with a diameter of 5 cm, it<br />

out.println("A circumference of " + (PI * 5) + "cm");<br />

out.println("And an area of " + (PI * pow(5,2)) + "sq. cm");

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!