File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 14074
    
  16

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
 
wood burning stoves
 
subject: Static Import in java 1.5
 
Similar Threads
flipping coin program
cant get counters to work correctly
Using Methods in methods
guessing game
Need help with doing a calculation in Java