Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Import in java 1.5

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic