This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static Import in java 1.5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Import in java 1.5" Watch "Static Import in java 1.5" New topic
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

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 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Import in java 1.5