The advantage of static imports

Hey,

that’s quite funny: having imported a package as static, you can use the constants it defines, as if you would have defined constants of your own class. So:

Without static import:

import java.util.concurrent.TimeUnit.*;

The above line forces us to explicitly import any of the constants that TimeUnit defines:

service.scheduleAtFixedRate(reminder, 0, 10, TimeUnit.SECONDS);

With static import:

import static java.util.concurrent.TimeUnit.*;

We only have to define the constant’s name:

service.scheduleAtFixedRate(reminder, 0, 10, SECONDS);

Cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s