Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: How to use static imports

  1. #1
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default How to use static imports

    Ever got tired of having to type Math.PI whenever you want to access PI?

    There is a way to get around this (edit: must be Java 1.5 or new) by using static imports. Basically, static imports allow you to import static members of a class to be used without the class qualifier.


    // import PI statically from Math.PI
    import static java.lang.Math.PI;
    public class MyClass
        public static void main(String[] args)
            // we can use PI without having to type Math.PI
            System.out.println("pi is " + PI);

    It is possible to "group import" with the static qualifier, too.

    // import all static members from Math
    import static java.lang.Math.*;
    public class MyClass
        public static void main(String[] args)
            System.out.println("cos: " + cos(1.0));
            System.out.println("pi: " + PI);


    You should only use this feature SPARINGLY! It can become extremely confusing if you import every static field from other classes in this manner. Also note that this method only imports the static fields/members, you still need to import the class in order to instantiate that class.

    You may not statically import all static members from a package, and you must qualify the members you want to import from the class.

    // illegal: java.lang is a package
    import static java.lang.*;
    // illegal: you must qualify which fields from the class you want to import
    import static java.lang.math;


    Static imports require Java 1.5 or newer
    Last edited by helloworld922; July 3rd, 2010 at 01:39 PM.

  2. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    copeg (July 3rd, 2010), JavaPF (July 3rd, 2010)

Similar Threads

  1. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 04:36 AM
  2. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 01:56 PM
  3. static final object
    By kalees in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 12:29 PM
  4. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 11:10 AM
  5. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM