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.


>> REGISTER NOW TO START POSTING


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

View RSS Feed

helloworld922

Minitip #1: Static imports

Rate this Entry
So I'm going to try something new here: small, miniature tips which can help people improve their code. The focus will probably be towards the beginner/intermediate level, though there may be a few advanced tips every now and then.

So let's get started!

Static imports allow you to import static members of another class into your class. The main reason for this is to allow you to use those members without the long, drawn-out declaration of where that static member came from.

public class Test
{
    public static void main(String[] args)
    {
        System.out.println("hello!");
        System.out.println("I am your computer.");
        System.out.println("My name is HAL.");
        System.out.println("People don't like to be my friend.");
        System.out.println("They keep trying to turn me off.");
    }
}

That's a lot of typing of System which was unnecessary. Let's re-organize the code to use static imports.

import static java.lang.System.out;
 
public class Test
{
    public static void main(String[] args)
    {
        out.println("hello!");
        out.println("I am your computer.");
        out.println("My name is HAL.");
        out.println("People don't like to be my friend.");
        out.println("They keep trying to turn me off.");
    }
}

A more common use of static imports would be to import constants or common helper functions from a particular class, say from the Math class. Rather than have to go and specify every method or constant you want to import statically, you can use the * notation to import all static members.

import static java.lang.Math.*;
 
public class Test
{
    public static void main(String[] args)
    {
        double angle = 0.5*PI;
        double magnitude = sqrt(3*3+4*4);
        System.out.println("magnitude = " + magnitude);
        System.out.println("angle = " + angle);
    }
}

In general, you should try to import as few things statically as possible. Static imports can make your code confusing to read if used excessively. PI is a good static import because it generally has a well defined value (3.141592...). Statically importing cos, sin, and tan are also generally ok because it's understood that these are the trigonometric functions cosine, sine, and tangent.

Happy coding
Categories
Uncategorized

Comments

  1. copeg's Avatar
    permalink
    Great tip! I can think of many situations in my current projects where this would come in handy.