Digital Edition

SYS-CON.TV
Java Basics: Lesson 11, Java Packages and Imports (Live Video Education)
Lesson 11 in the Hugely Popular "Java Basics" Series by JDJ Editorial Board Member Yakov Fain

Java 5.0 has introduced a new element called static imports. While regular import statements allow you to use the class name without specifying its package name, static imports go further and allow using static members of another class without specifying their class name. Let's use our good old HelloWorld program as an example:

public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello World");

}
}

Based on this syntax, we can guess that there is a static variable out in the class System that represents some object that in turn has a static method println(). Let's add the static import to eliminate the need of specifying the name of the class System in front of the variable out:

import static java.lang.System.*;
public class HelloWorld {

public static void main(String[] args) {
out.println("Hello World");

}
}

You do not have to use the wildcard (*) and import all static members from a class, but rather specific ones, for example

import static java.lang.System.out;

or for the PI constant from the Math package it can look like this:

import static java.lang.Math.PI;

For example:

import static java.lang.System.out;
import static java.lang.Math.PI;

public class HelloWorld {

public static void main(String[] args) {
out.println("Hello World123");
out.println("Hello PI: " + PI);
}
}

The good news is that if you need to call the println() method multiple times, your program becomes shorter now.

The bad side effect of static imports is that if you'll start overusing this feature, pretty soon your program will actually become less readable. For example, if you'll use static imports of multiple constants from various packages, and someone else (or even yourself five months later) will need to read this program, it may become difficult to understand where all these constants are coming from, especially if you've been using wildcards. The last HelloWorld example gives you an impression that PI constant has been declared somewhere in the HelloWorld class itself. In a small program it's easy to see that this is not the case, but in more complex applications consisting of hundreds of classes this can cause unnecessary confusions.

Prior to Java 5.0 you could have created Java interfaces containing only final static variables, for example

public interface MyConstants {
  public static final String companyName="XYZ, Inc.";
  public static final String companyAddress="123 Main st.";
}

Now any class that implements MyConstants can use this variables without even mentioning the name of the interface:

class Invoice implements MyConstants{
  public static void main(){
       System.out.println("Name:"+ companyName);
       System.out.println("Address: "+ companyAddress);
   }
}

Strictly speaking, if a class implements an interface, you'd expect it to implement some behavior which is not the case in the above example. That's why it's not a recommended way of using interfaces. Oh well, unless you have multiple interfaces with hundreds of constants, I do not see any damage in use of static imports or constant-only interfaces.

About Yakov Fain
Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

I'm glad you mentioned the use of interfaces as repositories of constants. While it may not be recommended in the strict sense of why interfaces exist, I've used this technique for years on large projects as a good way of organizing and reusing constants.

So you can use wildcards in the import? Can you use static import for static classes?

Will the other 10 lessons be on SYS-CON.TV?

Is it Yakov Fain who is speaking at the moment? A live Java lesson - cool!

I used this lesson in conjunction with the last one, lesson ten on eclipse IDE and it really helped me




ADS BY GOOGLE
Subscribe to the World's Most Powerful Newsletters

ADS BY GOOGLE

"Calligo is a cloud service provider with data privacy at the heart of what we do. We are a typical ...
Having been in the web hosting industry since 2002, dhosting has gained a great deal of experience w...
NanoVMs is the only production ready unikernel infrastructure solution on the market today. Unikerne...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO Silicon Valley 2019 will cover all of these tools, with the m...
SUSE is a German-based, multinational, open-source software company that develops and sells Linux pr...
Your job is mostly boring. Many of the IT operations tasks you perform on a day-to-day basis are rep...
Technological progress can be expressed as layers of abstraction - higher layers are built on top of...
When building large, cloud-based applications that operate at a high scale, it’s important to mainta...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, disc...
Big Switch's mission is to disrupt the status quo of networking with order of magnitude improvements...
Lori MacVittie is a subject matter expert on emerging technology responsible for outbound evangelism...
Dynatrace is an application performance management software company with products for the informatio...
In his session at 21st Cloud Expo, Michael Burley, a Senior Business Development Executive in IT Ser...
All in Mobile is a mobile app agency that helps enterprise companies and next generation startups bu...
Yottabyte is a software-defined data center (SDDC) company headquartered in Bloomfield Township, Oak...
Serveless Architectures brings the ability to independently scale, deploy and heal based on workload...
Whenever a new technology hits the high points of hype, everyone starts talking about it like it wil...
Every organization is facing their own Digital Transformation as they attempt to stay ahead of the c...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (No...
Chris Matthieu is the President & CEO of Computes, inc. He brings 30 years of experience in developm...