Java Reference
In-Depth Information
called a nonstatic method. Similarly, the heading of a method may contain the reserved
word public . In this case, it is called a public method. An important property of a
public and static method is that (in a program) it can be used (called) using the name
of the class, the dot operator, the method name, and the appropriate parameters. For
example, all the methods of the class Math are public and static . Therefore, the
general syntax to use a method of the class Math is:
Math.methodName(parameters)
(Note that, in fact, the parameters used in a method call are called actual parameters.) For
example, the following expression determines 2.5 3.5 :
Math.pow(2.5, 3.5)
(In the previous statement, 2.5 and 3.5 are actual parameters.) Similarly, if a method of the
class Character is public and static , you can use the name of the class ,whichis
Character , the dot operator, the method name, and the appropriate parameters. The
methods of the class Character listed in Table 7-2 are public and static .
To simplify the use of ( public ) static methods of a class, Java 5.0 introduces the
following import statements:
7
import static pakageName.ClassName.*; //to use any (public)
//static method of the class
import static packageName.ClassName.methodName; //to use a
//specific method of the class
These are called static import statements. After including such statements in your
program, when you use a ( public ) static method (or any other public static
member) of a class , you can omit the name of the class and the dot operator.
For example, after including the import statement:
import static java.lang.Math.*;
you can determine 2.5 3.5 by using the expression:
pow(2.5, 3.5)
After including the static import statement, in reality, you have a choice. When you use
a( public ) static method of a class , you can either use the name of the class and the
dot operator or omit them. For example, after including the static import statement:
import static java.lang.Math.*;
in a program, you can determine 2.5 3.5 by using either the expression
Math.pow(2.5, 3.5) or the expression pow(2.5, 3.5) .
The static import statement is not available in versions of Java lower than 5.0.
Therefore, if you are using, say, Java 4.0, then you must use a static method of the
class Math using the name of the class and the dot operator.
 
Search WWH ::




Custom Search