| Author |
Static Import in java 1.5
|
Rajendra Prakash
Ranch Hand
Joined: Sep 10, 2009
Posts: 293
|
|
No need for developer to import java.lang package. JVM loads it by default.
I know static import is used to static members of a class.
consider this situation
import static java.lang.Math.*;
public class StaticImportDemo
{
public static void main(String args[])
{
double pi = PI;
double randomNumber = random();
System.out.println("PI = " + pi);
System.out.println("Random = " + randomNumber);
}
}
In this above code , why java.lang package imported .
I dont want to use double pi = Math.PI;
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
Please UseCodeTags when you post source code.
Indeed, everything in the java.lang package is imported by default. But "import static" is something different, and it is not so that the contents of all classes and interfaces in java.lang is automatically imported.
The java.lang.Math class contains only static methods for mathematical functions (sin, cos, etc.). By statically importing the content of the Math class, you don't have to write "Math.random()" all the time, you can just write "random()".
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Static Import in java 1.5
|
|
|