T13: Knowledge Tip Of The Day...
Static import
static import is a feature that expands the capabilities of
import keyword. It is used to import static member of a class. We all know that
static member are referred in association with its class name outside the
class. Using static import, it is possible to refer to the static member
directly without its class name.
There are two general form of static import statement.
The first form of static import statement, import only a single
static member of a class
Syntax
import static package.class-name.static-member-name;
import static package.class-name.static-member-name;
Example
import static java.lang.Math.sqrt; //importing static method
sqrt of Math class
The second form of static import statement,imports all the
static member of a class
Syntax
import static package.class-type-name.*;
import static package.class-type-name.*;
Example
import static java.lang.Math.*; //importing all static member of
Math class
Example using static import ::-
import static java.lang.Math.*;
public class Test
{
public static void main(String[] args)
{
System.out.println(sqrt(144));
}
}
import static java.lang.Math.*;
public class Test
{
public static void main(String[] args)
{
System.out.println(sqrt(144));
}
}
No comments:
Post a Comment