File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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: 15092

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:
subject: Static Import in java 1.5
jQuery in Action, 3rd edition